Skip to content

Instantly share code, notes, and snippets.

@charlieInDen
Created August 1, 2020 06:44
Show Gist options
  • Save charlieInDen/f3d9693c8452a6f7ce9057ef6515fd83 to your computer and use it in GitHub Desktop.
Save charlieInDen/f3d9693c8452a6f7ce9057ef6515fd83 to your computer and use it in GitHub Desktop.
func lengthOfLIS(_ nums: [Int]) -> Int {
if nums.isEmpty { return 0 }
let len = nums.count
var dp = Array(repeating: 0, count: len)
dp[0] = 1
var ans = 1
for i in 1..<dp.count {
var maxVal = 0
for j in 0..<i {
if nums[i] > nums[j] {
maxVal = max(maxVal, dp[j])
}
}
dp[i] = maxVal + 1
ans = max(ans, dp[i])
}
return ans
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment