Skip to content

Commit a2f51e1

Browse files
add: 나머지 문제풀이
1 parent eab191b commit a2f51e1

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-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+
}
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)