分割数组为连续子序列

输入一个按升序排序的整数数组(可能包含重复数字),你需要将它们分割成几个子序列,其中每个子序列至少包含三个连续整数。返回你是否能做出这样的分割?

示例 1:

输入: [1,2,3,3,4,5]

输出: True

解释:

你可以分割出这样两个连续子序列 :

1, 2, 3

3, 4, 5

示例 2:

输入: [1,2,3,3,4,4,5,5]

输出: True

解释:

你可以分割出这样两个连续子序列 :

1, 2, 3, 4, 5

3, 4, 5

示例 3:

输入: [1,2,3,4,4,5]

输出: False

提示:

  1. 输入的数组长度范围为 [1, 10000]

 class Solution {
public boolean isPossible(int[] nums) {
Counter count = new Counter();
Counter tails = new Counter();
for (int x: nums) count.add(x, 1); for (int x: nums) {
if (count.get(x) == 0) {
continue;
} else if (tails.get(x) > 0) {
tails.add(x, -1);
tails.add(x+1, 1);
} else if (count.get(x+1) > 0 && count.get(x+2) > 0) {
count.add(x+1, -1);
count.add(x+2, -1);
tails.add(x+3, 1);
} else {
return false;
}
count.add(x, -1);
}
return true;
}
} class Counter extends HashMap<Integer, Integer> {
public int get(int k) {
return containsKey(k) ? super.get(k) : 0;
} public void add(int k, int v) {
put(k, get(k) + v);
}
}

Leetcode 659.分割数组为连续子序列的更多相关文章

  1. Java实现 LeetCode 659 分割数组为连续子序列 (哈希)

    659. 分割数组为连续子序列 输入一个按升序排序的整数数组(可能包含重复数字),你需要将它们分割成几个子序列,其中每个子序列至少包含三个连续整数.返回你是否能做出这样的分割? 示例 1: 输入: [ ...

  2. LeetCode——数组篇:659. 分割数组为连续子序列

    659. 分割数组为连续子序列 输入一个按升序排序的整数数组(可能包含重复数字),你需要将它们分割成几个子序列,其中每个子序列至少包含三个连续整数.返回你是否能做出这样的分割? 示例 1: 输入: [ ...

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

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

  4. 《github一天一道算法题》:分治法求数组最大连续子序列和

    看书.思考.写代码. /*************************************** * copyright@hustyangju * blog: http://blog.csdn. ...

  5. leetcode 410. 分割数组的最大值(二分法)

    1. 题目描述 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小. 注意: 数组长度 n 满足以下条件: 1 ≤ n ...

  6. Leetcode 410.分割数组的最大值

    分割数组的最大值 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小. 注意:数组长度 n 满足以下条件: 1 ≤ n ...

  7. Java实现 LeetCode 410 分割数组的最大值

    410. 分割数组的最大值 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小. 注意: 数组长度 n 满足以下条件: ...

  8. LeetCode 410——分割数组的最大值

    1. 题目 2. 解答 此题目为 今日头条 2018 AI Camp 5 月 26 日在线笔试编程题第二道--最小分割分数. class Solution { public: // 若分割数组的最大值 ...

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

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

随机推荐

  1. hbase的coprocessor使用(转)

    http://www.360doc.com/content/13/0320/09/4675893_272623864.shtml

  2. fcn

    上几周把fcn跑了几个模型,唉,因此测试程序,整了很久,浪费时间啊. fcn做分割,其实我是想用来做检测的,但是总是觉得这个框架是以后的趋势,所以一直想要去在这个基础上做个东西,目前训练的模型还没有测 ...

  3. SVN中trunk,branches,tags用法详解(转载)

    转载出处:http://www.cnblogs.com/dafozhang/archive/2012/06/28/2567769.html Subversion是一个自由开源的版本控制系统.在Subv ...

  4. datatable设置动态宽度,超过一定长度出现滚动条

    获得宽度:var tableAutoWidth = $('.dataTable_wrapper').width();if (tableAutoWidth < 1200) { tableAutoW ...

  5. yii2邮箱发送

    yii2 邮件发送  163邮箱 1.在配置文件main-local.php components=>[]里面配置 'mailer' => [ 'class' => 'yii\swi ...

  6. 用PHP关于Jquery表单插件ajaxForm里success不返回问题

    简单说一下吧,在用ajaxForm的时候,sucess突然之间不返回了,直接转到error里面去, 网页代码 ................. $('#add-type').ajaxForm({ d ...

  7. PHP处理mysql事务

    MYSQL的事务处理主要有两种方法.1.用begin,rollback,commit来实现begin 开始一个事务rollback 事务回滚commit 事务确认2.直接用set来改变mysql的自动 ...

  8. OC中block作方法参数时的用法

    方式一.在传参时直接声明block回调方法. 1. 定义方法: - (int)doTest:(NSString *)name para1:(int)temp1 para2:(int)temp2 suc ...

  9. 硬件中断--DEBUG系列

    问题描述: 在线调试时,全速运行,程序进入硬件中断,查看堆栈窗口,发现是从A函数进去的.但是A函数应该没有问题的: 再次重复,发现是从B函数进去的,但是B函数之前运行起来也没有问题的,而且没有传入参数 ...

  10. storm集群安装部署

    安装步骤: 搭建Zookeeper集群: 安装Storm依赖库: 下载并解压Storm发布版本: 修改storm.yaml配置文件: 启动Storm各个后台进程. 1. 搭建Zookeeper集群 这 ...