Skip to content

Commit 3984463

Browse files
committed
Create 0300-longest-increasing-subsequence.swift
1 parent 9a27b95 commit 3984463

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Diff for: swift/0300-longest-increasing-subsequence.swift

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
func lengthOfLIS(_ nums: [Int]) -> Int {
3+
var dp = [Int](repeating: 1, count: nums.count)
4+
for i in stride(from: nums.count - 1, to: -1, by: -1) {
5+
for j in (i + 1)..<nums.count {
6+
if nums[i] < nums[j] {
7+
dp[i] = max(dp[i], 1 + dp[j])
8+
}
9+
}
10+
}
11+
return dp.max() ?? -1
12+
}
13+
}

0 commit comments

Comments
 (0)