Skip to content

Commit

Permalink
[DP] Refactor the solution to Longest Increasing Subsequence
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Sep 22, 2019
1 parent dcdf431 commit 36a55b1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
38 changes: 27 additions & 11 deletions DP/LongestIncreasingSubsequence.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
/**
* Question Link: https://leetcode.com/problems/longest-increasing-subsequence/
* Primary idea: Dynamic Programming, transition function is len[i] = max(len[i], len[j] + 1)
* Time Complexity: O(n^2), Space Complexity: O(n)
* Primary idea: Dynamic Programming, update the array which ends at current index using binary search
* Time Complexity: O(nlogn), Space Complexity: O(n)
*/

class LongestIncreasingSubsequence {
func lengthOfLIS(nums: [Int]) -> Int {
var length_global = 0
var length_current = [Int](count: nums.count, repeatedValue: 1)
func lengthOfLIS(_ nums: [Int]) -> Int {
guard let first = nums.first else {
return 0
}

var ends = [first]

for i in 0..<nums.count {
for j in 0..<i {
if nums[i] > nums[j] {
length_current[i] = max(length_current[i], length_current[j] + 1)
for i in 1..<nums.count {

// find first greater ends number
var left = 0, right = ends.count

while left < right {
let mid = (right - left) / 2 + left

if ends[mid] < nums[i] {
left = mid + 1
} else {
right = mid
}
}
length_global = max(length_global, length_current[i])

if right >= ends.count {
ends.append(nums[i])
} else {
ends[right] = nums[i]
}
}

return length_global
return ends.count
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
[Maximum Sum of 3 Non-Overlapping Subarrays](https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/)| [Swift](./DP/MaximumSumThreeNonOverlappingSubarrays.swift)| Hard| O(n)| O(n)|
[Coin Change](https://leetcode.com/problems/coin-change/)| [Swift](./DP/CoinChange.swift)| Medium| O(n^2)| O(n)|
[Coin Change II](https://leetcode.com/problems/coin-change-ii/)| [Swift](./DP/CoinChangeII.swift)| Medium| O(n^2)| O(n)|
[Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/)| [Swift](./DP/LongestIncreasingSubsequence.swift)| Medium| O(n^2)| O(n)|
[Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/)| [Swift](./DP/LongestIncreasingSubsequence.swift)| Medium| O(nlogn)| O(n)|
[Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/)| [Swift](./DP/PalindromicSubstrings.swift)| Medium| O(n^2)| O(n^2)|
[Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)| [Swift](./DP/LongestPalindromicSubstring.swift)| Medium| O(n^2)| O(n^2)|
[Perfect Squares](https://leetcode.com/problems/perfect-squares/)| [Swift](./DP/PerfectSquares.swift)| Medium| O(n^2)| O(n)|
Expand Down

0 comments on commit 36a55b1

Please sign in to comment.