Skip to content

Commit 8dd6cc3

Browse files
authored
Merge pull request #1736 from jinvicky/main
[jinvicky] WEEK 02 solutions
2 parents fd05c30 + b88df03 commit 8dd6cc3

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

โ€Ž3sum/jinvicky.javaโ€Ž

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
// https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ๋‹ค์‹œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.
6+
class Solution {
7+
public List<List<Integer>> threeSum(int[] nums) {
8+
List<List<Integer>> res = new ArrayList<>();
9+
Arrays.sort(nums);
10+
11+
for (int i = 0; i < nums.length; i++) {
12+
if (i > 0 && nums[i] == nums[i-1]) {
13+
continue;
14+
}
15+
16+
int j = i + 1;
17+
int k = nums.length - 1;
18+
19+
while (j < k) {
20+
int total = nums[i] + nums[j] + nums[k];
21+
22+
if (total > 0) {
23+
k--;
24+
} else if (total < 0) {
25+
j++;
26+
} else {
27+
res.add(Arrays.asList(nums[i], nums[j], nums[k]));
28+
j++;
29+
30+
while (nums[j] == nums[j-1] && j < k) {
31+
j++;
32+
}
33+
}
34+
}
35+
}
36+
return res;
37+
}
38+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int climbStairs(int n) {
3+
// n=1์ผ๋•Œ ๋ฐฉ๋ฒ•์€ 1๊ฐ€์ง€๋‹ค.
4+
// n=2์ผ๋•Œ ๋ฐฉ๋ฒ•์€ 2๊ฐ€์ง€๋‹ค.
5+
// n=3์ผ๋•Œ ๋ฐฉ๋ฒ•์€ 3๊ฐ€์ง€๋‹ค.
6+
// n=4์ผ๋•Œ ๋ฐฉ๋ฒ•์€ 5๊ฐ€์ง€๋‹ค.
7+
// 1,1,1,1
8+
// 1,1,2
9+
// 1,2,1
10+
// 2,1,1
11+
// 2,2
12+
// dp ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ 4๋Š” 2์™€ 3์˜ ๋ฐฉ๋ฒ• ๊ฐœ์ˆ˜๋ฅผ ์žฌํ™œ์šฉํ•ด์„œ dp[n] = dp[n-2] + dp[n-1] ์„ ๋„์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.
13+
// ๋‹จ n์ด 1,2,3์ผ ๋•Œ์˜ ๊ฐ’์„ ๋จผ์ € ์…‹ํŒ…ํ•œ๋‹ค.
14+
15+
if (n <= 3)
16+
return n;
17+
18+
int[] dp = new int[n];
19+
dp[0] = 1;
20+
dp[1] = 2;
21+
dp[2] = 3;
22+
23+
for (int i = 3; i < n; i++) {
24+
dp[i] = dp[i - 1] + dp[i - 2];
25+
}
26+
27+
return dp[n - 1];
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// prefix sum ํŒจํ„ด์„ ์‚ฌ์šฉํ•œ๋‹ค.
2+
// nums.length ๊ธธ์ด์˜ ์ •๋‹ต ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  left, right ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค.
3+
// ์ฒ˜์Œ๋ถ€ํ„ฐ for๋ฌธ์œผ๋กœ nums๋งŒ ๋ˆ„์ ํ•ฉ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ–ˆ๋˜ ๊ฒŒ ์‹คํŒจ ์›์ธ์ด์—ˆ๋‹ค. ๋ณ€์ˆ˜ ์‚ฌ์šฉ ๋ฐ ๊ณ„์‚ฐ์‹์„ ๊ณ ๋ คํ•˜์ง€ ๋ชปํ•จ.
4+
class Solution {
5+
public int[] productExceptSelf(int[] nums) {
6+
int[] output = new int[nums.length];
7+
for (int i = 0; i < nums.length; i++) {
8+
output[i] = 1;
9+
}
10+
11+
// ์ •๋‹ต์— ๊ฐ’์„ ์ ์šฉํ•˜๊ณ  left, right์„ ์—…๋ฐ์ดํŠธํ•œ๋‹ค๋Š” ์ ์ด ์ค‘์š”ํ–ˆ๋‹ค. -> ์„ ์ ์šฉ ํ›„์—…๋ฐ์ดํŠธ๋ฅผ ์ž˜๋ชป ์ดํ•ดํ•ด์„œ ํ—ค๋งธ๋‹ค.
12+
int left = 1;
13+
for (int i = 0; i < nums.length; i++) {
14+
output[i] *= left; // 1,2,2,6
15+
left *= nums[i]; // 1,2,6,24 (output ์ ์šฉ์ด ๋๋‚ฌ์œผ๋ฏ€๋กœ 24๋Š” ์‚ฌ์‹ค์ƒ ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค)
16+
}
17+
18+
int right = 1;
19+
for (int i = nums.length - 1; i >= 0; i--) {
20+
output[i] *= right; // 6,8,12,24 -> ๋ฐฐ์—ด ์ธ๋ฑ์Šค๊ฐ€ ์—ญ์ˆœ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ์ ์œผ๋กœ [24, 12, 8, 6] ์ด ๋œ๋‹ค.
21+
right *= nums[i]; //
22+
}
23+
return output;
24+
}
25+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.Arrays;
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
5+
class Solution {
6+
//์œ ํšจํ•œ ์• ๋„ˆ๊ทธ๋žจ์˜ ์กฐ๊ฑด์ด ๋ฌด์—‡์ผ๊นŒ?
7+
//1. a์™€ b์˜ ์•ŒํŒŒ๋ฒณ๋ณ„ ๋นˆ๋„์ˆ˜๊ฐ€ ์ •ํ™•ํžˆ ๋™์ผํ•ด์•ผ ํ•œ๋‹ค.
8+
//2. ๋‹จ a์™€ b์˜ ์•ŒํŒŒ๋ฒณ ๊ตฌ์„ฑ ์ˆœ์„œ๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค.
9+
10+
// map ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•ด์„œ s์˜ ์•ŒํŒŒ๋ฒณ ๋นˆ๋„์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ณ  ์ดํ›„ t๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ map์— ์กด์žฌํ•˜๋Š” ์•ŒํŒŒ๋ฒณ์ผ ๊ฒฝ์šฐ
11+
// 1. ๋นˆ๋„์ˆ˜๋ฅผ ๊นŽ๋Š”๋‹ค. 2. ๋นˆ๋„์ˆ˜๊ฐ€ 0์ผ ๊ฒฝ์šฐ map์—์„œ ์‚ญ์ œํ•œ๋‹ค.
12+
public boolean isAnagramByHashMap(String s, String t) {
13+
Map<Character, Integer> map = new HashMap<>();
14+
for (char c : s.toCharArray()) {
15+
map.put(c, map.getOrDefault(c, 0) + 1);
16+
}
17+
18+
for (char c : t.toCharArray()) {
19+
Integer v = map.get(c);
20+
21+
if (v == null)
22+
return false;
23+
24+
if (v - 1 == 0) {
25+
map.remove(c);
26+
} else
27+
map.put(c, v - 1);
28+
}
29+
return map.size() < 1;
30+
}
31+
32+
// ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ๋‹จ์ˆœํžˆ ๋‘ ๋ฌธ์ž์—ด์„ ์ •๋ ฌํ•˜๊ณ  ๋ฌธ์ž์—ด ๋‚ด์šฉ ์ผ์น˜ ๋น„๊ต๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.
33+
// ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ๋•Œ ์ฒ˜์Œ์—๋Š” ๋ฌด์กฐ๊ฑด ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ–ˆ์œผ๋‚˜,
34+
// ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด 1๋ฒˆ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•˜๋ฏ€๋กœ ๊ธธ์ด ๋น„๊ต ๋กœ์ง์„ ์ •๋ ฌ ์ „์— ์ถ”๊ฐ€ํ•จ์œผ๋กœ์„œ ์‹œ๊ฐ„ ์„ฑ๋Šฅ์„ ๋†’์˜€๋‹ค.
35+
public boolean isAnagram(String s, String t) {
36+
if (s.length() != t.length()) return false;
37+
char[] sChars = s.toCharArray();
38+
char[] tChars = t.toCharArray();
39+
40+
Arrays.sort(sChars);
41+
Arrays.sort(tChars);
42+
43+
return new String(sChars).equals(new String(tChars));
44+
}
45+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
// dfs ๋ฐฉ์‹์œผ๋กœ ํ’€์ดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. tree ๊ด€๋ จ easy ๋ฌธ์ œ๋ฅผ 10๋ฌธ์ œ ์ •๋„ ํ’€๊ณ  ์ ‘๊ทผํ–ˆ์Šต๋‹ˆ๋‹ค.
3+
class Solution {
4+
public boolean isValidBST(TreeNode root) {
5+
return check(root, Long.MIN_VALUE, Long.MAX_VALUE);
6+
}
7+
8+
private boolean check(TreeNode node, long min, long max) {
9+
if (node == null) return true;
10+
11+
if (!(node.val > min && node.val < max)) return false;
12+
13+
return check(node.left, min, node.val) && check(node.right, node.val, max);
14+
}
15+
}

0 commit comments

Comments
ย (0)