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]
Approach #1: C++.
class Solution {
public:
bool isPossible(vector<int>& nums) {
int pre = nums[0] - 1;
int a1 = 0, a2 = 0, a3 = 0;
for (int i = 0; i < nums.size(); ) {
int j = i;
while (j+1 < nums.size() && nums[j+1] == nums[j]) ++j;
int cnt = j - i + 1;
int cur = nums[i];
if (cur != pre + 1) {
if (a1 != 0 || a2 != 0) return false;
a3 = 0;
a1 += cnt;
} else {
int b1 = 0, b2 = 0, b3 = 0;
if (a1 > cnt) return false;
b2 += a1, cnt -= a1, a1 = 0;
if (a2 > cnt) return false;
b3 += a2, cnt -= a2, a2 = 0;
b3 += min(a3, cnt), cnt -= min(cnt, a3);
a1 = cnt;
a2 = b2;
a3 = b3;
} pre = cur;
i = j + 1; } return a1 == 0 && a2 == 0;
}
};
Analysis:
In this problem we use a1, a2, a3 represent the number of the subsequences with the length of 1, 2, 3.
cnt represent the number of same elements in this loop.
pre represent the number in the last time loop we force on (nums[i-1]).
first : we should judge if the array is consequent with the pre number and cur number. if so, we continue the next step, otherwise, we should judge if a1 and a2 equal to 0.
second : we should put the cur number in to the previous subsequences with the length of 1 or 2. if at this loop the same numbers (cnt) smaller than a1 or a2, this means that in the next loop we will have subsequences' length less than 3, so we should return false; otherwise, we update the value of a1, a2 and a3.
finlly : we judge if a1 == 0 and a2 == 0.
Approach #2: Java.
class Solution {
public boolean isPossible(int[] nums) {
int pre = Integer.MIN_VALUE, p1 = 0, p2 = 0, p3 = 0;
int cur = 0, cnt = 0, c1 = 0, c2 = 0, c3 = 0; for (int i = 0; i < nums.length; pre = cur, p1 = c1, p2 = c2, p3 = c3) {
for (cur = nums[i], cnt = 0; i < nums.length && cur == nums[i]; cnt++, i++);
if (cur != pre + 1) {
if (p1 != 0 || p2 != 0) return false;
c1 = cnt; c2 = 0; c3 = 0;
} else {
if (cnt < p1 + p2) return false;
c1 = Math.max(0, cnt - (p1 + p2 + p3));
c2 = p1;
c3 = p2 + Math.min(p3, cnt - (p1 + p2));
}
} return (p1 == 0 && p2 == 0);
}
}
659. Split Array into Consecutive Subsequences的更多相关文章
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- [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 ...
- [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 ...
- [Swift]LeetCode659. 分割数组为连续子序列 | Split Array into Consecutive Subsequences
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
随机推荐
- POJ3111(最大化平均值)
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 8458 Accepted: 2163 Case Time ...
- [转]ubuntu 网络配置 作者:Yudar
检查网络配置命令:ifconfig 一.通过配置文件配置 新手没怎么用过Ubuntu,所以走了不少弯路,网上找了很多方法,大都没对我起到帮助作用,所以把自己的配置方法写一写. Ubuntu上连了两块网 ...
- Python 中的0 和 1 的意思
Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false,所以Python中的 1 代表 True,0代表False
- linux命令-vim
vim是vi的升级版 //////////////////////////////////////////////////////////////////////////////// 首先安装vim ...
- dubbo-Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
dubbo-2.8.4需用jdk版本为1.8,dubbo-2.5.3可以使用1.7版本的jdk.
- adb pull 和 adb push
1. 操作单个文件 通过adb push,则可将文件添加到SD卡中.如果想在push的时候修改文件名称的话,只需要修改push的第二个参数改成完整路径(目录+文件名),如/sdcard/test-0. ...
- php二维数组排序方法(array_multisort,usort)
一维数组排序可以使用asort.ksort等一些方法进程排序,相对来说比较简单.二维数组的排序怎么实现呢?使用array_multisort和usort可以实现 例如像下面的数组: $users = ...
- [patl2-014]列车调度
解题关键:由Dilworth定理(最小反链划分 == 最长链)可知最少的下降序列个数就等于整个序列最长上升子序列的长度,此题即转化为求最长上升子序列的长度. #include<cstdio> ...
- cocos2d-js 序列帧动画
var spriteCache = cc.spriteFrameCache;spriteCache.addSpriteFrames(res.fireworks_plist,res.fireworks_ ...
- 关于A类,B类,C类IP地址的网段和主机数的计算方法
关于A类,B类,C类IP地址的网段和主机数的计算方法 IP地址是一个32位的二进制数,由四个八位字段组成.每个IP地址包括两部分:一部分为网络标识(网络号),一部分为主机标识(主机号). A类地址前8 ...