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);
}
}

  

最新文章

  1. 后进先出 stack、 先进先出Queue
  2. PHP 如何获取当前的域名
  3. Codeforces Round #355 (Div. 2)-B
  4. CF #374 (Div. 2) C. Journey dp
  5. SQLite常用命令
  6. CREATE DATABASE permission denied in database &#39;master&#39;.
  7. 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇06:移动版优化指南》--本系列完结
  8. 【Error listenerStart】 Error listenerStart Context [] startup failed due to previous errors
  9. Java新手入门必须掌握的30个基本概念
  10. VMware下安装Ubuntu,那么必须安装VMware-tools,才能获得更好的体验,包括屏幕分辨率、声音、和windows共享剪贴板等等
  11. C++设计模式——抽象工厂模式
  12. 快速开发 HTML5 WebGL 的 3D 斜面拖拽生成模型
  13. ZOJ Problem Set - 3708 Density of Power Network
  14. SAP配置BOM的适用范围
  15. Android 第二波
  16. .NET MVC 后台接受base64的上传图片
  17. MySQL普通用户无法本地登录的解决方法及MySQL的用户认证算法
  18. 细说GIT分布式版本控制器
  19. ETL技巧应用(高级应用介绍:准备区运用、 时间戳的运用、日志表的运用、使用调度)
  20. CPP2-基础部分(1)

热门文章

  1. Task Crontab
  2. Sass、Less和Stylus
  3. Intellij IDEA 发布后的项目在哪里
  4. maven学习6 Eclipse下Tomcat常用设置
  5. 【转】火狐浏览器中firebug插件的时间线域解释
  6. python对MySQL进行数据的插入、更新和删除之后需要commit,数据库才会真的有数据操作。(待日后更新)
  7. MFC简单的橡皮筋程序
  8. #测试两种不同的SVM,rbf的核真是太棒了(一种会拐弯的边界)
  9. C#理解泛型(源代码)及 default(T)
  10. springmvc中针对一个controller方法配置两个url请求