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:

  1. 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的更多相关文章

  1. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  2. [LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  3. leetcode 659. Split Array into Consecutive Subsequences

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  4. [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 ...

  5. 【leetcode】659. Split Array into Consecutive Subsequences

    题目如下: 解题思路:本题可以维护三个字典,dic_1保存没有组成序列的单元素,dic_2保存组成了包含两个元素的序列中的较大的元素,dic_3保存组成了包括三个或者三个以上元素的序列中的最大值.因为 ...

  6. Split Array into Consecutive Subsequences

    659. Split Array into Consecutive Subsequences You are given an integer array sorted in ascending or ...

  7. leetcode659. Split Array into Consecutive Subsequences

    leetcode659. Split Array into Consecutive Subsequences 题意: 您将获得按升序排列的整数数组(可能包含重复项),您需要将它们拆分成多个子序列,其中 ...

  8. [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  9. [Swift]LeetCode659. 分割数组为连续子序列 | Split Array into Consecutive Subsequences

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

随机推荐

  1. Azure VM Disk的设计与部署

    Azure的VM的设计中,Disk相关的设计是非常重要的一个内容,本文将介绍Azure上的VM的Disk相关的一些最佳实践和一些小的技巧. 一.Azure VM中Disk的存储账户设计 1. Stor ...

  2. 关于C#客户端引用C++ dll的问题

    近期在做项目的过程中需要在Winform客户端项目引用由C++编译的DLL,于是对相关的内容进行了一些研究,有几点心得总结如下. 第一步是制作要引用的类库: (1)首先拿到C++的dll,需要注意的是 ...

  3. L2-013. 红色警报(dfs)

    L2-013. 红色警报 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 战争中保持各个城市间的连通性非常重要.本题要求你编写一 ...

  4. 修改crushmap实验

    标签(空格分隔): ceph,ceph实验,crushmap CRUSH的全称是Controlled Replication Under Scalable Hashing,是ceph数据存储的分布式选 ...

  5. 问题:oracle long 与 clob;结果:long类型比clob到底差在什么地方?

    long类型比clob到底差在什经常看到9i以上的文档,说以后 clob会逐步取代long,一直想不出, 而我在8.1.7上也测试2个字段好像 在存储上也看不出什么区别?么地方? 差别还是很大的, 比 ...

  6. LAMP 2.9 php扩展模块如何安装

    php 和 apache 类似,核心文件为/usr/local/php/bin/php,针对 apache 的是/usr/local/apache2/modules/libphp5.so 模块.这两个 ...

  7. pl/sql对excel数据的导入和导出

    本来这部分是在上篇pl/sql的,但笔者介于此篇的内容,就独立出来了, 1.导出查询结果到excel文件,在查询结果上右键,然后弹出选择框如下: 2.从excel向数据库中导入数据: a.创建要导入的 ...

  8. DAY10-MYSQL表操作

    一 存储引擎介绍 存储引擎即表类型,mysql根据不同的表类型会有不同的处理机制 http://www.cnblogs.com/guoyunlong666/p/8491702.html 二 表介绍 表 ...

  9. 【MySQL】20个经典面试题(转)

    原文链接:http://bbs.51cto.com/thread-1470880-1.html Part2:经典题目 1.MySQL的复制原理以及流程 基本原理流程,3个线程以及之间的关联: 2.my ...

  10. sql server导入excel等数据

    1.首先打开并登陆sql server数据库 2.选择要将表导入的数据库,右击选择任务-->导入数据 3.在弹出的窗口中选择下一步 4.在弹出的窗口中选择数据源,也就是从哪种文件导入,sql s ...