leetcode 659. Split Array into Consecutive Subsequences
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个连续的数作为一个分割,并记录每个分割最后的一个数(为后期拓展所用)。
第二次扫描,看剩下的数能不能放到任何一个分割的后面。如果能,那么更新分割的最后一个值。
第三次扫描,看还有没有剩下的数字,如果没有那么返回true,否则false
看似天一无缝,却始终过不了倒数第二个测试例子------> 179 / 180 test cases passed.
手写了一个样例 [1,2,3,4,5,5,5,6,6,7,7,8,9,10]
在处理的时候,按照上上面的套路(1,2,3)(4,5,6),(5,6,7),(7,8,9) 剩下 5和10, 5,和10 只能放在 3,6,7,9后面,10可以放在9后面,但是5没有地方放,返回false。
可是 我们肉眼能找到(1,2,3,4,5)(5,6,7)(5,6,7)(8,9,10)这样合法的分割,应该返回true。思路有瑕疵。其实我们思考的顺序有错误,对于当前的数字x,我们应该先判断x-1是否存在,如果存在就直接放上就好了,不存在的时候再构建一个长度为3个分割,如果长度为3的分割都构建不了,那么直接返回false就ok了。说道这里,这题目还是有贪心的味道的....
这里mp2[y]记录以y作为i分割末尾的数量。
class Solution {
public:
bool isPossible(vector<int>& nums) {
int n = nums.size();
//sort(nums.begin(), nums.end());
if (n < ) return false;
map<int, int>mp;
for (int i = ; i < n; ++i) mp[nums[i]]++; map<int, int>mp2;
for (int i = ; i < n; ++i) {
int x = nums[i];
if (mp[x] <= ) continue;
if (mp2[x-] > ) {
mp2[x]++, mp2[x-]--, mp[x]--;
} else if (mp[x+] > && mp[x+] > ) {
mp[x]--, mp[x+]--, mp[x+]--;
mp2[x+]++;
} else {
return false;
}
}
return true;
}
};
leetcode 659. Split Array into Consecutive Subsequences的更多相关文章
- [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 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 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] Split Array into Consecutive Subsequences 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- LeetCode Split Array into Consecutive Subsequences
原题链接在这里:https://leetcode.com/problems/split-array-into-consecutive-subsequences/description/ 题目: You ...
随机推荐
- 修改centos的yum源为国内的源
1.安装Centos后默认的Yum源如下 ll /etc/yum.repos.d/ [root@localhost ~]# ll /etc/yum.repos.d/ total 32 -rw-r- ...
- OI中的小智慧
反正不会咕咕的. sort之类没+1的问题不说 双向边n*2的问题不说 变量n+5的问题不说 1.先生成后判断 (见NOIP 2016 pj t2回文日期) 这个思想在这道题体现的不明显,记得洛谷上面 ...
- 【01】sass基础信息:
[01]基础信息: 2016年4月16日 最新版本:3.4.22 官网:http://sass-lang.com/(下图) CSStoSCSS 网站:http://css2sa ...
- 【Codeforces 25C】Roads in Berland
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 用floyd思想. 求出来这条新加的边影响到的点对即可. 然后尝试更新点对之间的最短路就好. 更新之后把差值从答案里面减掉. [代码] #in ...
- Leetcode 313.超级丑数
超级丑数 编写一段程序来查找第n个超级丑数. 超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数. 示例: 输入: n = 12, primes = [2,7,13,19] ...
- ubuntu使用git时,终端不显示git分支。
1:问题描述: 在Windows环境下习惯使用git bash操作git分支,最近学习linux环境,发现linux环境终端不显示git分支,相关现象如下: 期望效果是: 我的linux环境 ...
- msp430入门编程21
msp430中C语言的扩展--#pragma编译命令
- 为什么zookeeper的节点配置的个数必须是奇数个?
zookeeper有这样一个特性:集群中只要有过半的机器是正常工作的,那么整个集群对外就是可用的.也就是说如果有2个zookeeper,那么只要有1个死了zookeeper就不能用了,因为1没有过半, ...
- topcoder 650 srm div2 1000pts
(15) 也是 DIV1 500 题意是给定 一个无向图 删去一条边以后 可不可以是完全二叉树. 细节点很多,开始做法居然求到桥去了,最近强联通写傻了. 最多1024-1个点 1024-1条边枚举 所 ...
- win10 笔记本猎豹WiFi无法打开
网卡驱动太新了,先把网卡驱动卸载,重新安装一个就可以,用驱动精灵,17.15.0.5版本就可以