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 ...
随机推荐
- 【HTML/XML 5】使用XSL给XML文档添加样式
导读:上篇博客中以具体实例分析了HTML和XML在语义上的不同,但是,大家也都发现,XML表现出来的,并没有HTML那样直观或者说美观.其原因是因为XML的表现内容和表现形式被分离.它的表现形式有两种 ...
- POJ 1006-Biorhythms,中国剩余定理,学信安的路过!
Biorhythms 我竟然1A了, 终于从一天的浑噩中找回点自信了.人生第一次做中国剩余定理的题 ...
- Linux内核设计与实现——读书笔记1:内核简介
内核:有的时候被称管理者或者操作系统核心,通常内核负责响应中断的中断服务程序, 负责管理多个进程从而分享处理器时间的调度程序,负责管理进程地址空间德内存管理程序 和网络,进程间通信等系统服务程序共同组 ...
- Android操作系统架构
Android操作系统架构 Android操作系统整体应用架构 Android系统架构和一些普遍的操作系统差不多,都是采用了分层的架构,从他们之间的架构图看,Android系统架构分为四个层,从高 ...
- django学习之- CSRF及中间件
CSRF # 表示django全局发送post请求均需要字符串验证功能:防止跨站请求伪造的功能工作原理:客户端访问服务器端,在服务器端正常返回给客户端数据的时候,而外返回给客户端一段字符串,等到客户端 ...
- 胜利大逃亡--hdu --1253(bfs)
Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C ...
- 最少拦截系统-----hdu1257(dp+最长上升子序列)
Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高 ...
- The Unique MST-POJ1679(次小生成树)
http://poj.org/problem?id=1679 次小生成树 #include<stdio.h> #include<string.h> #include<st ...
- java nio实现非阻塞Socket通信实例
服务器 package com.java.xiong.Net17; import java.io.IOException; import java.net.InetSocketAddress; imp ...
- Python中文GBK编码解决实例
http://eatsalt.blog.163.com/blog/static/879402662009420508748/ #coding:gbk l=['我'.decode('gbk'),'我'. ...