Skip to content

Commit fc43e4c

Browse files
Merge pull request #1232 from YoungSeok-Choi/feature/week-2
[YoungSeok-Choi] Week 2 Solutions
2 parents 97dee73 + 5616672 commit fc43e4c

File tree

5 files changed

+262
-0
lines changed

5 files changed

+262
-0
lines changed

3sum/YoungSeok-Choi.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.Collections;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
// NOTE: 실행시간 1.5초..
9+
// 시간 복잡도: O(n^2)
10+
class Solution {
11+
public List<List<Integer>> threeSum(int[] nums) {
12+
13+
Arrays.sort(nums);
14+
Set<List<Integer>> result = new HashSet<>();
15+
16+
for(int i = 0; i < nums.length - 2; i++) {
17+
int startIdx = i + 1;
18+
int endIdx = nums.length - 1;
19+
while(startIdx < endIdx) {
20+
int sum = nums[i] + nums[startIdx] + nums[endIdx];
21+
if(sum == 0) {
22+
result.add(Arrays.asList(nums[i], nums[startIdx], nums[endIdx]));
23+
}
24+
25+
if(sum > 0) {
26+
endIdx--;
27+
} else {
28+
startIdx++;
29+
}
30+
}
31+
}
32+
33+
return new ArrayList<>(result);
34+
}
35+
}
36+
37+
38+
39+
// 309 / 314 testcases passed Time Limit Exceeded
40+
// NOTE: 시간복잡도 O(n^3)
41+
class WrongSolution {
42+
public List<List<Integer>> threeSum(int[] nums) {
43+
Set<List<Integer>> res = new HashSet<>();
44+
45+
List<Integer> temp = new ArrayList<>();
46+
47+
temp.remove(3);
48+
49+
for(int i = 0; i < nums.length; i++) {
50+
for(int j = 0; j < i; j++) {
51+
for(int k = 0; k < j; k++) {
52+
53+
if((nums[i] + nums[j] + nums[k]) == 0) {
54+
List<Integer> solution = Arrays.asList(nums[i], nums[j], nums[k]);
55+
Collections.sort(solution);
56+
res.add(solution);
57+
}
58+
}
59+
}
60+
}
61+
62+
63+
return new ArrayList<>(res);
64+
}
65+
}

climbing-stairs/YoungSeok-Choi.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// NOTE: 백준 N과 M문제와 거의 동일한 문제
2+
// memo(n) = memo(n-1)의 경우에 수에서 1칸을 올라가는 경우 + memo(n-2) 의 경우의 수에서 2 칸을 올라가는 경우의 점화식으로 표현됨.
3+
// 시간 복잡도: O(n)
4+
class Solution {
5+
public int climbStairs(int n) {
6+
int[] memo = new int[n + 1];
7+
8+
9+
if(n < 3) {
10+
return n;
11+
}
12+
13+
memo[1] = 1;
14+
memo[2] = 2;
15+
for(int i = 3; i < memo.length; i++) {
16+
memo[i] = memo[i - 1] + memo[i - 2];
17+
}
18+
19+
return memo[n];
20+
}
21+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
7+
// O(nlogn) 시간복잡도.
8+
9+
class Solution {
10+
public int[] topKFrequent(int[] nums, int k) {
11+
Map<Integer, Integer> freqMap = new HashMap<>();
12+
int[] res = new int [k];
13+
14+
for(int i = 0; i < nums.length; i++) {
15+
if(freqMap.containsKey(nums[i])) {
16+
freqMap.put(nums[i], freqMap.get(nums[i]) + 1);
17+
} else {
18+
freqMap.put(nums[i], 1);
19+
}
20+
}
21+
22+
List<Map.Entry<Integer, Integer>> entList = new ArrayList<>(freqMap.entrySet());
23+
24+
entList.sort((a, b) -> b.getValue().compareTo(a.getValue()));
25+
26+
for(int i = 0; i < res.length; i++) {
27+
res[i] = entList.get(i).getKey();
28+
}
29+
30+
return res;
31+
}
32+
}

valid-anagram/YoungSeok-Choi.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
5+
// NOTE: anagram 자체를 정확하게 이해하지 못해서 몇 번 틀렸던 문제.
6+
// 두 문자열의 길이가 다르면 anagram이 아니다.
7+
// 같은 알파뱃이 같은 개수가 있는지 확인을 하는게 문제의 핵심.
8+
9+
// NOTE: 시간복잡도: O(n+m)
10+
class Solution {
11+
public boolean isAnagram(String s, String t) {
12+
Map<Character, Integer> sMap = new HashMap<>();
13+
Map<Character, Integer> tMap = new HashMap<>();
14+
15+
int tLen = t.toCharArray().length;
16+
int sLen = s.toCharArray().length;
17+
18+
if(tLen != sLen) {
19+
return false;
20+
}
21+
22+
for(char c: s.toCharArray()) {
23+
if(sMap.containsKey(c)) {
24+
int cnt = sMap.get(c);
25+
sMap.put(c, cnt + 1);
26+
} else {
27+
sMap.put(c, 1);
28+
}
29+
}
30+
31+
for(char c: t.toCharArray()) {
32+
if(tMap.containsKey(c)) {
33+
int cnt = tMap.get(c);
34+
tMap.put(c, cnt + 1);
35+
} else {
36+
tMap.put(c, 1);
37+
}
38+
}
39+
40+
41+
for(Character c: sMap.keySet()) {
42+
if(!tMap.containsKey(c)) {
43+
return false;
44+
}
45+
46+
int sMapCnt = sMap.get(c);
47+
int tMapCnt = tMap.get(c);
48+
49+
if(sMapCnt != tMapCnt) {
50+
return false;
51+
}
52+
}
53+
54+
return true;
55+
}
56+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* public class TreeNode {
8+
* int val;
9+
* TreeNode left;
10+
* TreeNode right;
11+
* TreeNode() {}
12+
* TreeNode(int val) { this.val = val; }
13+
* TreeNode(int val, TreeNode left, TreeNode right) {
14+
* this.val = val;
15+
* this.left = left;
16+
* this.right = right;
17+
* }
18+
* }
19+
*/
20+
// NOTE: 답지를 보고 풀었던 문제.. 정확하게 이해하는 과정이 필요하다.
21+
// TODO: 별도 블로그로 풀이과정을 정리할 것.
22+
class Solution {
23+
public boolean isValidBST(TreeNode root) {
24+
return dfs(root, Long.MIN_VALUE, Long.MAX_VALUE);
25+
}
26+
27+
public boolean dfs(TreeNode cur, long min, long max) {
28+
if(cur == null) return true;
29+
30+
if(cur.val <= min || cur.val >= max) return false;
31+
32+
return dfs(cur.left, min, cur.val) && dfs(cur.right, cur.val, max);
33+
}
34+
}
35+
36+
class WrongSolution {
37+
public boolean isValidBST(TreeNode root) {
38+
return dfs(root, new ArrayList<>(Arrays.asList(root.val)));
39+
}
40+
41+
public boolean dfs(TreeNode cur, List<Integer> path) {
42+
43+
if(cur.left != null) {
44+
int lVal = cur.left.val;
45+
for(int i = 0; i < path.size(); i++) {
46+
if(i == path.size() - 1) {
47+
48+
if(lVal >= path.get(i)) {
49+
return false;
50+
}
51+
52+
} else {
53+
if(lVal < path.get(i)) {
54+
return false;
55+
}
56+
}
57+
}
58+
59+
path.add(cur.left.val);
60+
if(!dfs(cur.left, path)) return false;
61+
path.remove(Integer.valueOf(cur.left.val));
62+
}
63+
64+
if(cur.right != null) {
65+
int rVal = cur.right.val;
66+
for(int i = 0; i < path.size(); i++) {
67+
if(i == path.size() - 1) {
68+
69+
if(rVal <= path.get(i)) {
70+
return false;
71+
}
72+
73+
74+
} else {
75+
if(rVal > path.get(i)) {
76+
return false;
77+
}
78+
}
79+
}
80+
81+
path.add(cur.right.val);
82+
if(!dfs(cur.right, path)) return false;
83+
path.remove(Integer.valueOf(cur.right.val));
84+
}
85+
86+
return true;
87+
}
88+
}

0 commit comments

Comments
 (0)