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 ...
随机推荐
- POJ 3122 pie (二分法)
Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have ...
- Java学习笔记(1)-(GridBagLayout)网格袋布局
学习JAVA-布局管理的时候,在书上看到了这么一段话:GridBagLayout的功能非常强大,使用是也比较复杂,考虑到一般的读者很少会使用到这种管理,这里不做介绍.然书本就跳过了,为什么功能强大却很 ...
- Google JavaScript代码风格指南
Google JavaScript代码风格指南 修正版本 2.28 Aaron Whyte Bob Jervis Dan Pupius Eric Arvidsson Fritz Schneider R ...
- 线性回归Linear regression
线性回归算法 解决回归问题 思想简单,容易实现 是许多强大的非线性模型的基础 结果具有很好的可解释性 蕴含机器学习中的很多重要思想 基本思想:寻找一条直线,最大程度的“拟合”样本特征和样本输出标记之间 ...
- HDU-2509-Be the Winner,博弈题~~水过~~
Be the Winner Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ht ...
- [luoguP3694] 邦邦的大合唱站队/签到题(状压DP)
传送门 来自kkk的题解: 70分做法:枚举每个学校顺序,暴力. 100分:状压dp.从队列头到尾DP, 状态:f[i]表示i状态下最小的出列(不一致)的个数. 比如f[1101]表示从头到位为1/3 ...
- POJ 2553 The Bottom of a Graph 【scc tarjan】
图论之强连通复习开始- - 题目大意:给你一个有向图,要你求出这样的点集:从这个点出发能到达的点,一定能回到这个点 思路:强连通分量里的显然都可以互相到达 那就一起考虑,缩点后如果一个点有出边,一定不 ...
- mysql免安装版配置使用
mysql免安装版配置使用 1.下载解压 2.配置环境变量 变量MYSQL_HOME = 解压目录 配置变量path 编辑,在后面加上 ;%MYSQL_HOME%\bin 3.修改配置文件 增加或 ...
- android GET 请求在5.0版本的取不到数据,报IO异常兼容问题解决
使用lib类库xUtils-2.6.10.jar作为数据请求的框架,在android的5.0版会有兼容问题,取不到GET请求的数据. 但是POST没有问题,难取到数据. public static R ...
- IIS文件存在但报404问题解决
遇到一个奇怪的问题,在IIS7.5中,一些样式和JS文件存在,但访问就是报404. 根据网上搜索到的解决方法,发现解决不了,不同同样的问题引起的. 网上解决: 1.没有配置合适的MIME信息,通过添加 ...