Skip to content

Commit 43f4b16

Browse files
committed
add longest increasing subsequence solution
1 parent ac05c98 commit 43f4b16

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* ์ •์ˆ˜ ๋ฐฐ์—ด nums๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ๊ฐ€์žฅ ๊ธธ๊ฒŒ ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ๊ธธ์ด๋ฅผ ์ฐพ์œผ์„ธ์š”. (LIS)
3+
*/
4+
class Solution {
5+
6+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n^2)
7+
public int lengthOfLIS(int[] nums) {
8+
9+
int[] dp = new int[nums.length];
10+
int n = nums.length;
11+
int max = 0;
12+
13+
// ์›์†Œ์˜ ์œ„์น˜์—์„œ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” LIS์˜ ๊ฐ’ ๊ณ„์‚ฐ
14+
for (int i = 0; i < n; i++) {
15+
dp[i] = 1;
16+
for (int j = 0; j < i; j++) {
17+
if (nums[j] < nums[i]) {
18+
dp[i] = Math.max(dp[i], dp[j] + 1);
19+
}
20+
}
21+
max = Math.max(dp[i], max);
22+
}
23+
24+
return max;
25+
}
26+
}
27+

0 commit comments

Comments
ย (0)