★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-oeclsaxa-kt.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given an unsorted array of integers, find the length of longest increasing subsequence.git
Example:github
Input: Output: 4 Explanation: The longest increasing subsequence is , therefore the length is . [10,9,2,5,3,7,101,18][2,3,7,101]4
Note:算法
Follow up: Could you improve it to O(n log n) time complexity?數組
給定一個無序的整數數組,找到其中最長上升子序列的長度。微信
示例:app
輸入: 輸出: 4 解釋: 最長的上升子序列是 它的長度是 。[10,9,2,5,3,7,101,18][2,3,7,101],4
說明:spa
進階: 你能將算法的時間複雜度下降到 O(n log n) 嗎?code
16mshtm
1 class Solution { 2 func lengthOfLIS(_ nums: [Int]) -> Int { 3 if nums.count < 2 { 4 return nums.count 5 } 6 var res = Array(repeating: 0, count: nums.count) 7 res[0] = nums[0] 8 var count = 1 9 var j = 0 10 for i in 0..<nums.count { 11 let tmp = nums[i] 12 if tmp > res[count - 1] { 13 j = count 14 count += 1 15 }else { 16 var left = 0 17 var right = count - 1 18 while left < right { 19 let mid = (left + right) / 2 20 if res[mid] >= nums[i] { 21 right = mid 22 }else { 23 left = mid + 1 24 } 25 } 26 j = left 27 } 28 res[j] = nums[i] 29 } 30 return count 31 } 32 }
56ms
1 class Solution { 2 func lengthOfLIS(_ nums: [Int]) -> Int { 3 var results = [Int]() 4 for num in nums { 5 var needAdd = true 6 if results.count > 0 { 7 for index in 0..<results.count { 8 if num <= results[index] { 9 results[index] = num 10 needAdd = false 11 break 12 } 13 } 14 } 15 if needAdd { 16 results.append(num) 17 } 18 } 19 return results.count 20 } 21 }
300ms
1 class Solution { 2 func lengthOfLIS(_ nums: [Int]) -> Int { 3 guard !nums.isEmpty else { 4 return 0 5 } 6 7 var dps = Array(repeating: 1, count: nums.count) 8 var result = 1 9 for index in 1..<nums.count { 10 let num = nums[index] 11 for j in 0..<index { 12 if num > nums[j] { 13 let length = dps[j] + 1 14 if length > dps[index] { 15 dps[index] = length 16 result = max(result, dps[index]) 17 } 18 } 19 } 20 } 21 22 return result 23 } 24 }
312ms
1 class Solution { 2 func lengthOfLIS(_ nums: [Int]) -> Int { 3 var length_global = 0 4 var length_current = [Int].init(repeating:1 , count: nums.count) 5 6 for i in 0..<nums.count { 7 for j in 0..<i { 8 if nums[i] > nums[j] { 9 length_current[i] = max(length_current[i], length_current[j] + 1) 10 } 11 } 12 length_global = max(length_global, length_current[i]) 13 } 14 15 return length_global 16 } 17 }
376ms
1 class Solution { 2 func lengthOfLIS(_ nums: [Int]) -> Int { 3 guard !nums.isEmpty else { 4 return 0 5 } 6 7 var dps = Array(repeating: 1, count: nums.count) 8 var result = 1 9 for index in 1..<nums.count { 10 let num = nums[index] 11 for j in 0..<index where num > nums[j] { 12 let length = dps[j] + 1 13 dps[index] = max(length, dps[index]) 14 result = max(result, dps[index]) 15 } 16 } 17 18 return result 19 } 20 }
388ms
1 class Solution { 2 func lengthOfLIS(_ nums: [Int]) -> Int { 3 4 if nums.count == 0 { 5 return 0 6 } 7 8 var result = 1 9 var dArr = [Int](repeating: 1, count: nums.count) 10 11 for i in 0..<nums.count { 12 13 for j in 0..<i { 14 15 if(nums[j] < nums[i] && (dArr[j] + 1 >= dArr[i])) { 16 dArr[i] = dArr[j] + 1 17 } 18 } 19 result = dArr[i] > result ? dArr[i] : result 20 } 21 return result 22 } 23 }