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 ...
随机推荐
- 【01】Firebug 教程
Firebug 教程 什么是 Firebug? Firebug 是一个开源的web开发工具. 现在浏览器自带firebug了. 安装 Firebug Firebug下载地址: https: ...
- SqlParameter[] parameters
SqlParameter[] parameters = { new SqlParameter("@rdTypeName", readertype.rdTypeName), new ...
- Java.lang.NoSuchMethodError: 后带 V/Z等字母的
知道 Java.lang.NoSuchMethodError: 后带 V/Z等字母的 错误,一般都是 jar包冲突引起的,找到冲突的jar包,去掉一个就好
- 【CSS】常见问题集锦
position=absolute 时,定位的父元素变成了body而不是父div?原因:如果父div的position为非static,则相对父div.参考:http://www.jianshu.co ...
- hihoCoder#1086 Browser Caching
原题地址 list+map可以轻松搞定,如果不借助STL实现起来还是挺麻烦的 代码: #include <iostream> #include <string> #includ ...
- (转)详解shell中>/dev/null 2>&1到底是什么
转 原文地址:https://blog.csdn.net/zouli415/article/details/80651526 前言 相信大家经常能在shell脚本中发现>/dev/null 2& ...
- python学习之-项目开发目录规范
软件目录结构规范有什么好处: 通过规范化,能够更好的控制软件结构,让程序具有更高的可读性. 项目目录组织结构如下: Foo/ # 项目名 --bin/ # 可执行文件目录 --foo # 可执行程序 ...
- [Bzoj4566][Haoi2016]找相同字符(广义后缀自动机)
4566: [Haoi2016]找相同字符 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 861 Solved: 495[Submit][Statu ...
- ATcoder 1983 BBQ Hard
E - BBQ Hard Time limit : 2sec / Memory limit : 256MB Score : 1400 points Problem Statement Snuke is ...
- 51nod 1499 (最小割)
题意 分析 将一些点分成两个集合,很明显的最小割问题 设一个S.T,和S相连的点表示在B集合中,和T相连的点表示在A集合中 因为原题是完美值最大,我们转换一下,变成损失的价值最小,那么就是最小割问题了 ...