[Swift]LeetCode659. 分割數組爲連續子序列 | Split Array into Consecutive Subsequences

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ytsboefg-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split.git

Example 1:github

Input: [1,2,3,3,4,5]
Output: True
Explanation:
You can split them into two consecutive subsequences : 
1, 2, 3
3, 4, 5 

Example 2:數組

Input: [1,2,3,3,4,4,5,5]
Output: True
Explanation:
You can split them into two consecutive subsequences : 
1, 2, 3, 4, 5
3, 4, 5 

Example 3:微信

Input: [1,2,3,4,4,5]
Output: False 

Note:app

  1. The length of the input is in range of [1, 10000]

輸入一個按升序排序的整數數組(可能包含重複數字),你須要將它們分割成幾個子序列,其中每一個子序列至少包含三個連續整數。返回你是否能作出這樣的分割? spa

示例 1:code

輸入: [1,2,3,3,4,5]
輸出: True
解釋:
你能夠分割出這樣兩個連續子序列 : 
1, 2, 3
3, 4, 5 

示例 2:htm

輸入: [1,2,3,3,4,4,5,5]
輸出: True
解釋:
你能夠分割出這樣兩個連續子序列 : 
1, 2, 3, 4, 5
3, 4, 5 

示例 3:blog

輸入: [1,2,3,4,4,5]
輸出: False 

提示:

  1. 輸入的數組長度範圍爲 [1, 10000]

620ms

 1 class Solution {
 2     func isPossible(_ nums: [Int]) -> Bool {
 3         var pre = 0
 4         var preCount = 0
 5         var starts = [Int]()
 6         var anchor = 0
 7         for i in 0..<nums.count {
 8             let t = nums[i]
 9             if i == nums.count - 1 || nums[i+1] != t {
10                 let count = i - anchor + 1
11                 if pre != 0 && (t - pre) != 1 {
12                     while preCount > 0 {
13                         if pre < (2 + starts.removeFirst()) {
14                             return false
15                         }
16                         preCount -= 1
17                     }
18                     pre = 0
19                 }
20 
21                 if pre == 0 || (t - pre) == 1{
22                     while preCount > count {
23                         preCount -= 1
24                         if (t-1) < (2 + starts.removeFirst()) {
25                             return false
26                         }
27                     }
28 
29                     while preCount < count {
30                         starts.append(t)
31                         preCount += 1
32                     }
33                 }
34 
35                 pre = t
36                 preCount = count
37                 anchor = i+1
38             }
39         }
40 
41         while preCount > 0 {
42             if nums[nums.count - 1] < (2 + starts.removeFirst()) {
43                 return false
44             }
45             preCount -= 1
46         }
47         return true
48     }
49 }

Runtime: 712 ms
Memory Usage: 19.4 MB
 1 class Solution {
 2     func isPossible(_ nums: [Int]) -> Bool {
 3         var freq:[Int:Int] = [Int:Int]()
 4         var need:[Int:Int] = [Int:Int]()
 5         for num in nums
 6         {
 7             freq[num,default:0] += 1
 8         }
 9         for num in nums
10         {
11             if freq[num,default:0] == 0
12             {
13                 continue
14             }
15             else if need[num,default:0] > 0
16             {
17                 need[num,default:0] -= 1
18                 need[num + 1,default:0] += 1
19             }
20             else if freq[num + 1,default:0] > 0 && freq[num + 2,default:0] > 0
21             {
22                 freq[num + 1,default:0] -= 1
23                 freq[num + 2,default:0] -= 1
24                 need[num + 3,default:0] += 1
25             }
26             else
27             {
28                 return false
29             }
30             freq[num,default:0] -= 1
31         }
32         return true
33     }
34 }
相關文章
相關標籤/搜索