659. 分割数组为连续子序列

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

示例 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]

************************************************************

问题分析:

  • 采用链表freq保存数组元素对应个数
  • 采用need用来判断当前元素是否能够插入到一个已经构建好的序列末端
     public boolean isPossible(int[] nums) {
//数组中每个元素与出现次数的对应关系
Map<Integer, Integer> freq = new HashMap<>();
Map<Integer, Integer> need = new HashMap<>(); for (int n : nums) freq.put(n, freq.getOrDefault(n, 0) + 1); for (int n: nums) {
if (freq.getOrDefault(n, 0) == 0) continue; if (need.getOrDefault(n, 0) > 0){
need.put(n, need.get(n) - 1);
need.put(n + 1, need.getOrDefault(n + 1, 0) + 1);
} else if (freq.getOrDefault(n + 1, 0) > 0 && freq.getOrDefault(n + 2, 0) > 0){
freq.put(n +1, freq.get(n + 1) - 1);
freq.put(n + 2, freq.get(n + 2) - 1);
need.put(n + 3, need.getOrDefault(n + 3, 0) + 1);
} else return false;
freq.put(n, freq.get(n) - 1);
} return true;
}

参考链接:https://blog.csdn.net/LaputaFallen/article/details/80034863

在测试时,发现有些条件题目没有给出提示,比如所有nums数组中的元素值大小都在±10000以内,所以也可以基于数组的形式完成,并且效率更高!

     //测试中发现nums元素值都在正负10000以内
public boolean isPossible(int[] nums) {
int[] counts = new int[20000];
int[] need = new int[20000]; for (int i = 0; i < nums.length; i ++) {
//将所有负数转为正数
nums[i] += 10000;
counts[nums[i]] ++;
} for (int n : nums) {
if (counts[n] == 0) continue; if (need[n] > 0){
need[n] --;
need[n + 1] ++;
} else if (counts[n + 1] > 0 && counts[n + 2] > 0){
counts[n + 1] --;
counts[n + 2] --;
need[n + 3] ++;
} else return false;
counts[n] --;
}
return true;
}

LeetCode——数组篇:659. 分割数组为连续子序列的更多相关文章

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

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

  2. Leetcode 659.分割数组为连续子序列

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

  3. js算法之把一个数组按照指定的数组大小分割成若干个数组块

    题目描述:     把一个数组arr按照指定的数组大小size分割成若干个数组块. 例如:   chunk([1,2,3,4],2)=[[1,2],[3,4]];   chunk([1,2,3,4,5 ...

  4. lintcode :最长上升连续子序列

    题目: 最长上升连续子序列 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列.(最长上升连续子序列可以定义为从右到左或从左到右的序列.) 样例 ...

  5. lintcode-397-最长上升连续子序列

    397-最长上升连续子序列 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列.(最长上升连续子序列可以定义为从右到左或从左到右的序列.) 注意事 ...

  6. lintcode_397_最长上升连续子序列

    最长上升连续子序列   描述 笔记 数据 评测 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列.(最长上升连续子序列可以定义为从右到左或从左到 ...

  7. 连续子序列的最大和 牛客网 剑指Offer

    连续子序列的最大和 牛客网 剑指Offer 题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量 ...

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

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

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

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

随机推荐

  1. 深入浅出SharePoint——使用WinDbg进行调试

  2. [COGS 2065]学数数

    2065. 学数数 ★★★☆   输入文件:jxthree.in   输出文件:jxthree.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 从前有一只咩,还有一只叽 ...

  3. Linux下中间人攻击利用框架bettercap测试

    0x01简介 bettercap可用来实现各种中间人攻击,模块化,便携.易扩展 0x02特点 提到中间人攻击,最知名的莫过于ettercap,而开发bettercap的目的不是为了追赶它,而是替代它 ...

  4. eclipse git 解决冲突

    1,team->synchronize workspace 2, merge tool 合并本地版本 3,add to git index 4,commit 5,push

  5. angularJs的工具方法3

    一.angular.version          判断angular的版本 console.log(angular.version); 二.angular.equals           判断两 ...

  6. BZOJ 3211 花神游历各国 线段树平方开根

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3211 题目大意: 思路: 由于数据范围只有1e9,一个数字x开根号次数超过logx之后 ...

  7. CF712D Memory and Scores

    题目分析 实际上两个人轮流取十分鸡肋,可以看作一个人取2t次. 考虑生成函数. 为了方便,我们对取的数向右偏移k位. 取2t次的生成函数为: \[ F(x)=(\sum_{i=0}^{2k}x_i)^ ...

  8. TP,TN,FP,FN

    一张图搞定~~~ [转]https://blog.csdn.net/u011956147/article/details/78967145

  9. 33、springboot整合springcloud

    Spring Cloud Spring Cloud是一个分布式的整体解决方案.Spring Cloud 为开发者提供了在分布式系统 (配置管理,服务发现,熔断,路由,微代理,控制总线,一次性token ...

  10. YourUninstaller注册码(可用)

    Name:Giveawayoftheday Registration code: 000017-2PNBK2-J59U6F-317E09-R5TGJQ-6B1WNA-AZCYNJ-GVP86A-7VP ...