【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
【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
参考资料:
日期
2018 年 8 月 29 日 ———— 还是要早起才行啊!
【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)的更多相关文章
- [LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- leetcode 659. Split Array into Consecutive Subsequences
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- 659. Split Array into Consecutive Subsequences
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- [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 ...
- 【leetcode】659. Split Array into Consecutive Subsequences
题目如下: 解题思路:本题可以维护三个字典,dic_1保存没有组成序列的单元素,dic_2保存组成了包含两个元素的序列中的较大的元素,dic_3保存组成了包括三个或者三个以上元素的序列中的最大值.因为 ...
- Split Array into Consecutive Subsequences
659. Split Array into Consecutive Subsequences You are given an integer array sorted in ascending or ...
- leetcode659. Split Array into Consecutive Subsequences
leetcode659. Split Array into Consecutive Subsequences 题意: 您将获得按升序排列的整数数组(可能包含重复项),您需要将它们拆分成多个子序列,其中 ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
随机推荐
- 脱离Editor、VS等IDE如何编译UE4工程
在Windows平台下,我们从.uproject文件生成VS解决方案.sln文件 .uproject文件用于打开Editor .sln文件用于打开VS工程 对于有增加C++代码的工程,Editor中和 ...
- 01 eclipse搭建maven的web工程(3.1)
eclipse搭建maven的web工程(3.1) 一.下载并在eclipse安装JDK环境[查看] 二.下载并在eclipse安装maven环境[查看] 三.新建maven-webapp工程: 1. ...
- .NET Core基础篇之:集成Swagger文档与自定义Swagger UI
Swagger大家都不陌生,Swagger (OpenAPI) 是一个与编程语言无关的接口规范,用于描述项目中的 REST API.它的出现主要是节约了开发人员编写接口文档的时间,可以根据项目中的注释 ...
- 日常Java 2021/9/20
Java随机数 运用Java的random函数实现猜数字游戏 随机产生一个1-50之间的数字,然后让玩家猜数,猜大猜小都给出提示,猜对后游戏停止 package pingchangceshi; imp ...
- 【Reverse】每日必逆0x01
附件:https://files.buuoj.cn/files/7458c5c0ce999ac491df13cf7a7ed9f1/SimpleRev 题解 查壳 64位ELF文件,无壳 IDApro处 ...
- 链栈(C++)
链栈,字面意思,就是用链表来实现一个栈的数据结构. 那么,只需将单链表的头节点当作栈顶,尾节点当作栈底.入栈只需要头插,出栈只需头删即可.所以只需要吧单链表稍微阉割一下就可以得到链式栈了.代码如下 / ...
- 转 序列化Serializable和Parcelable的区别详解
什么是序列化,为什么要进行序列化 答:对象要进行传输(如:activity 与activity间 ,网络间 进程间等等).存储到本地就必须进行序列化 . 这种可传输的状态就是序列化. 怎么序列化??两 ...
- oracle 拆分字符串
WITH t AS (SELECT '1-2-3-4' a FROM dual)SELECT Regexp_Substr(a, '[^-]+', 1, LEVEL) i FROM tCONNECT B ...
- linux 配置本地yum
1.挂载光盘 #挂载光盘 mount /dev/cdrom /mnt/cdrom 2.修改yum.conf, 运行 vi /etc/yum.conf,文件替换成如下内容 [main] cachedir ...
- get请求url参数中有+、空格、=、%、&、#等特殊符号的问题解决
url出现了有+,空格,/,?,%,#,&,=等特殊符号的时候,可能在服务器端无法获得正确的参数值,如何是好?解决办法将这些字符转化成服务器可以识别的字符,对应关系如下:URL字符转义 用其它 ...