Skip to content

Commit b56f126

Browse files
authored
Merge pull request #1760 from ohgyulim/main
[ohgyulim] WEEK 02 solutions
2 parents ce26df6 + 9ae391a commit b56f126

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
/* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(N^2)
5+
* - for ๋ฃจํ”„: O(N)
6+
* - while ๋ฃจํ”„: O(N) -> N^2
7+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(K), answer์˜ K = List<Integer> ๊ฐœ์ˆ˜
8+
*/
9+
public List<List<Integer>> threeSum(int[] nums) {
10+
Arrays.sort(nums);
11+
12+
List<List<Integer>> answer = new ArrayList<>();
13+
for (int i = 0; i < nums.length - 2; i++) {
14+
if (i > 0 && nums[i] == nums[i - 1]) continue;
15+
16+
int left = i + 1;
17+
int right = nums.length - 1;
18+
19+
while (left < right) {
20+
int sum = nums[i] + nums[left] + nums[right];
21+
if (sum == 0) {
22+
answer.add(List.of(nums[i], nums[left], nums[right]));
23+
while (left < right && nums[left] == nums[left + 1]) left += 1;
24+
while (left < right && nums[right] == nums[right - 1]) right -= 1;
25+
left += 1;
26+
right -= 1;
27+
} else if (sum < 0) left += 1;
28+
else right -= 1;
29+
}
30+
}
31+
32+
return answer;
33+
}
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
/* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(N)
3+
* - for ๋ฃจํ”„: O(N)
4+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(N), dp๋ฐฐ์—ด
5+
*/
6+
public int climbStairs(int n) {
7+
if (n == 1) return 1;
8+
int[] dp = new int[n + 1];
9+
dp[1] = 1;
10+
dp[2] = 2;
11+
for (int i = 3; i <= n; i++) {
12+
dp[i] = dp[i - 1] + dp[i - 2];
13+
}
14+
15+
return dp[n];
16+
}
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
/* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(N)
3+
* - for ๋ฃจํ”„: O(N)
4+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(N), answer ๋ฐฐ์—ด
5+
*/
6+
public int[] productExceptSelf(int[] nums) {
7+
int product = 1;
8+
int zeroCnt = 0;
9+
for (int num : nums) {
10+
if (num == 0) {
11+
zeroCnt += 1;
12+
continue;
13+
}
14+
product *= num;
15+
}
16+
17+
int[] answer = new int[nums.length];
18+
if (zeroCnt > 1) {
19+
return answer;
20+
}
21+
22+
for (int i = 0; i < nums.length; i++) {
23+
int num = nums[i];
24+
if (zeroCnt == 1) {
25+
if (num == 0) answer[i] = product;
26+
} else {
27+
answer[i] = product / num;
28+
}
29+
}
30+
return answer;
31+
}
32+
}
33+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
/* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(N)
3+
* - for ๋ฃจํ”„: O(N)
4+
*
5+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(52)
6+
*/
7+
public boolean isAnagram(String s, String t) {
8+
if (s.length() != t.length()) return false;
9+
10+
int[] sCounts = new int[26];
11+
int[] tCounts = new int[26];
12+
for (int i = 0; i < s.length(); i++) {
13+
sCounts[s.charAt(i) - 'a'] += 1;
14+
tCounts[t.charAt(i) - 'a'] += 1;
15+
}
16+
17+
for (int i = 0; i < 26; i++) {
18+
if (sCounts[i] != tCounts[i]) return false;
19+
}
20+
21+
return true;
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
/* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(N)
3+
* - ์žฌ๊ท€ ํ˜ธ์ถœ: ํŠธ๋ฆฌ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜(N) ๋งŒํผ
4+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(N)
5+
* - ์žฌ๊ท€ ํ˜ธ์ถœ: ํŠธ๋ฆฌ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜(N) ๋งŒํผ
6+
*/
7+
public boolean isValidBST(TreeNode root) {
8+
return validate(root, Long.MIN_VALUE, Long.MAX_VALUE);
9+
}
10+
11+
boolean validate(TreeNode node, long min, long max) {
12+
if (node == null) return true;
13+
if (node.val <= min || node.val >= max) return false;
14+
return validate(node.left, min, node.val) && validate(node.right, node.val, max);
15+
}
16+
}
17+

0 commit comments

Comments
ย (0)