Skip to content

Commit b4b8026

Browse files
add: 증가하는 최대 부분수열
1 parent 0d5b434 commit b4b8026

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import java.util.Arrays;
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
5+
// tc: O(n)
6+
// idea: max largest subseq length for "N" th array index
7+
class Solution {
8+
9+
public int lengthOfLIS(int[] nums) {
10+
11+
int[] arr = new int[nums.length];
12+
Arrays.fill(arr, 1);
13+
14+
for(int i = 1; i < nums.length; i++) {
15+
for(int j = 0; j < i; j++) {
16+
if(nums[j] < nums[i]) {
17+
arr[i] = Math.max(arr[i], arr[j] + 1);
18+
}
19+
}
20+
}
21+
22+
int mx = -987654321;
23+
for(int anInt : arr) {
24+
System.out.print(anInt + " ");
25+
mx = Math.max(mx, anInt);
26+
}
27+
28+
return mx;
29+
}
30+
}
31+
32+
// time limit exceed
33+
34+
class WrongSolution {
35+
36+
public boolean[] visit;
37+
public int mxVal = -987654321;
38+
public int mx = 1;
39+
public Map<Integer, Boolean> m = new HashMap<>();
40+
41+
public int lengthOfLIS(int[] nums) {
42+
for(int i = 0; i < nums.length; i++) {
43+
m = new HashMap<>();
44+
mxVal = Math.max(mxVal, nums[i]);
45+
m.put(nums[i], true);
46+
dfs(i, nums);
47+
mxVal = -987654321;
48+
}
49+
50+
return mx;
51+
}
52+
53+
public void dfs(int idx, int[] nums) {
54+
mx = Math.max(mx, m.size());
55+
56+
for(int i = idx + 1; i < nums.length; i++) {
57+
if(mxVal < nums[i]) {
58+
int prev = mxVal;
59+
mxVal = nums[i];
60+
m.put(nums[i], true);
61+
62+
dfs(i, nums);
63+
64+
mxVal = prev;
65+
m.remove(nums[i]);
66+
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)