# 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告（Python）

1033 篇文章 86 订阅
901 篇文章 198 订阅

## 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告（Python）

id： fuxuemingzhu

### 题目描述：

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.

Example 1:

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:

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

### 解题方法

1，2，3，3，4，4，5，5

num last            len     current         map
1   null->(0,[ ])   0       (1, [1])    (0,[ ] ) (1, [1])
2   (1, [1])        1       (2, [2])    (0,[ ] ) (1, [ ])(2, [2])
3   (2, [2])        2       (3, [3])    (0,[ ] ) (1, [ ])(2, [ ])(3, [3])
3   (2, [ ])        0       (3, [1])    (0,[ ] ) (1, [ ])(2, [ ])(3, [3])(3, [1])
4   (3, [1])        1       (4, [2])    (0,[ ] ) (1, [ ])(2, [ ])(3, [3])(3, [ ])(4, [2])
4   (3, [3])        3       (4, [4])    (0,[ ] ) (1, [ ])(2, [ ])(3, [ ])(3, [ ])(4, [2])(4, [4])
5   (4, [2])        2       (5, [3])    (0,[ ] ) (1, [ ])(2, [ ])(3, [ ])(3, [ ])(4, [ ])(4, [4])(5, [3])
5   (4, [4])        4       (5, [5])    (0,[ ] ) (1, [ ])(2, [ ])(3, [ ])(3, [ ])(4, [ ])(4, [ ])(5, [3])(5, [5])


class Solution(object):
def isPossible(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
saved = collections.defaultdict(list)
for num in nums:
last = saved[num - 1]
_len = 0 if (not last) else heapq.heappop(last)
current = saved[num]
heapq.heappush(current, _len + 1)
for values in saved.values():
for v in values:
if v < 3:
return False
return True


1. https://blog.csdn.net/sunday0904/article/details/78174122

### 日期

2018 年 8 月 29 日 ———— 还是要早起才行啊！

• 1
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
12-06 435
05-03 942
09-02 1406

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。