Skip to content

Commit 6aa76e4

Browse files
committed
Solution Longest increasing subsequence
1 parent b0dc1f1 commit 6aa76e4

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
func lengthOfLIS(_ nums: [Int]) -> Int {
3+
if nums.isEmpty {
4+
return 0
5+
}
6+
7+
let n = nums.count
8+
var dp = Array(repeating: 1, count: n)
9+
10+
for i in 1..<n {
11+
for j in 0..<i {
12+
if nums[i] > nums[j] {
13+
dp[i] = max(dp[i], dp[j] + 1)
14+
}
15+
}
16+
}
17+
18+
return dp.max() ?? 1
19+
}
20+
}

0 commit comments

Comments
 (0)