【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/split-array-into-consecutive-subsequences/description/

题目描述:

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]

题目大意

把一个升序的数组,分割成几个连续的递增的整数序列。如果能分割,且分割后的每个序列的长度都至少为3,那么认为成功,否则失败。

解题方法

这就是所谓的扑克牌算法,必须全部弄成“顺子”。一个“顺子”至少3张连续的牌。方法是使用优先级队列,优先把当前的牌放入到更短的“顺子”里(贪心)。

这个题的思想就是贪心+优先级队列

首先判断以(num-1)为结尾的序列是否存在,

如果存在,获取长度最小值len并出栈,创建以num为结尾的数组,并设置长度为len + 1,推入优先队列;

如果不存在,创建新的序列,以num为结尾,并且长度为1,推入优先队列,创建新的键值对(num,currentPriorityQueue)推入map中。

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 日 ———— 还是要早起才行啊!

【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)的更多相关文章

  1. [LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  2. leetcode 659. Split Array into Consecutive Subsequences

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  3. 659. Split Array into Consecutive Subsequences

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  4. [LC] 659. Split Array into Consecutive Subsequences

    Given an array nums sorted in ascending order, return true if and only if you can split it into 1 or ...

  5. 【leetcode】659. Split Array into Consecutive Subsequences

    题目如下: 解题思路:本题可以维护三个字典,dic_1保存没有组成序列的单元素,dic_2保存组成了包含两个元素的序列中的较大的元素,dic_3保存组成了包括三个或者三个以上元素的序列中的最大值.因为 ...

  6. Split Array into Consecutive Subsequences

    659. Split Array into Consecutive Subsequences You are given an integer array sorted in ascending or ...

  7. leetcode659. Split Array into Consecutive Subsequences

    leetcode659. Split Array into Consecutive Subsequences 题意: 您将获得按升序排列的整数数组(可能包含重复项),您需要将它们拆分成多个子序列,其中 ...

  8. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  9. [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

随机推荐

  1. 10 — springboot整合mybatis — 更新完毕

    1.xml版 -- 复杂sql使用xml,简单sql使用注解 1).导入依赖 <!-- mybatis-spring-boot-starter是第三方( mybatis )jar包,不是spri ...

  2. typedef定义数组

    typedef定义数组 问题来源 在学习高一凡数据结构与算法解析串这一章节时,遇到如下代码不明白其意义,经过查阅终于搞明白 typedef unsigned char SString[MAXLEN + ...

  3. A Child's History of England.11

    CHAPTER 4 ENGLAND UNDER ATHELSTAN AND THE SIX BOY-KINGS Athelstan, the son of Edward the Elder, succ ...

  4. Vue 之keep-alive的使用,实现页面缓存

    什么是keep-alive 有时候我们不希望组件被重新渲染影响使用体验: 或者处于性能考虑,避免多次重复渲染降低性能.而是希望组件可以缓存下来,维持当前的状态.这时候就需要用到keep-alive组件 ...

  5. 删除数据库时报错 ERROR 1010 (HY000): Error dropping database (can't rmdir './cart', errno: 39)

    这是因为在数据目录下有表相关的数据(不是表),此时应该进入存放表的目录下删除与表相关的数据,一般数据存放目录默认为/var/lib/mysql,cd到目录下 执行命令:cd /var/lib/mysq ...

  6. web端 - 返回上一步,点击返回,跳转上个页面 JS

    1.方法一: <script language="javascript" type="text/javascript"> window.locati ...

  7. 1.ElasticSearch相关概念

    1.为ElasticSearch设置跨域访问 http.cors.enabled: truehttp.cors.allow-origin: "*" 2.什么是ElasticSear ...

  8. angular过滤器在html和js中的使用

    在HTML中使用格式为:{{数据 | 过滤器名称:条件一:条件二--}}:过滤条件间使用:隔开 例如: 在代码中一般格式为:  变量 = $filter("过滤器名称")(被过滤数 ...

  9. I/O流之字节流

    在程序中所有的数据都是以流的形式进行传输或保存的,程序需要数据时要使用输入流读取数据,而当程序需要将一些数据保存起来时,就要使用输出流完成对于操作文件内容,要进行文件内容的操作就需要通过Java提供的 ...

  10. Redis集群到集群迁移

    目录 一.物理导入 简介 实际操作 一.物理导入 简介 redis集群在存储数据时,是根据槽点进行存储.例如老集群A如下: 都在一台机器,实际可以在多台机器上. 主节点:7000(0-5460) 70 ...