659. 分割数组为连续子序列
给你一个按升序排序的整数数组 num(可能包含重复数字),请你将它们分割成一个或多个子序列,其中每个子序列都由连续整数组成且长度至少为 3 。
如果可以完成上述分割,则返回 true ;否则,返回 false 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/split-array-into-consecutive-subsequences
著作权归领扣网络所有。
示例 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, 10000]
自己的做法,参考官方答案,虽然通过,但是效率不高
执行结果:通过
执行用时:5 ms, 在所有 Java 提交中击败了89.23%的用户
内存消耗:39.1 MB, 在所有 Java 提交中击败了92.18%的用户
class Solution {
public boolean isPossible(int[] nums) {
int n=nums.length;
n=nums[n-1]-nums[0]+1;//因为可能有负数,尾数减去头数可以算出有多少数字
int[][] data = new int[n+1][2];
//Arrays.fill(data,0);
for(int i=0;i<=n;i++){
data[i][0]=0;//二维数组来存储,第一维表示每个数还剩多少个没有排序
data[i][1]=0;//第二维表示以当前数字结尾的序列个数
}
for(int i=0;i<nums.length;i++){
data[nums[i]-nums[0]+1][0]++;//将所有数据从1开始存储
}
for(int i=0;i<=n;){
if(data[i][0]==0){
i++;
continue;
}else{
//先判断有没有data[i-1][0]的序列,
if(data[i-1][1]>0){//由于所有数据是从1开始存储的,0始终为0,不会越界
data[i][1]++;//如果有i-1结尾的序列,将i加入该序列,以i结尾的序列加1
data[i-1][1]--;//以i-1结尾的序列减1
data[i][0]--;//i的数量减1
continue;
}
if(i+2>n){
//不足3个数,返回false
return false;
}
if(data[i+1][0]>0 && data[i+2][0]>0){
data[i+2][1]++;//以i+2结尾的序列加1,序列长度为3
data[i][0]--;//i的数量减1
data[i+1][0]--;//i+1的数量减1
data[i+2][0]--;//i+2的数量减1
}else{
//不足3个数,返回false
return false;
}
}
}
return true;
}
}
官方答案
https://leetcode-cn.com/problems/split-array-into-consecutive-subsequences/solution/fen-ge-shu-zu-wei-lian-xu-zi-xu-lie-by-l-lbs5/
方法一:哈希表 + 最小堆
class Solution {
public boolean isPossible(int[] nums) {
Map<Integer, PriorityQueue<Integer>> map = new HashMap<Integer, PriorityQueue<Integer>>();
for (int x : nums) {
if (!map.containsKey(x)) {
map.put(x, new PriorityQueue<Integer>());
}
if (map.containsKey(x - 1)) {
int prevLength = map.get(x - 1).poll();
if (map.get(x - 1).isEmpty()) {
map.remove(x - 1);
}
map.get(x).offer(prevLength + 1);
} else {
map.get(x).offer(1);
}
}
Set<Map.Entry<Integer, PriorityQueue<Integer>>> entrySet = map.entrySet();
for (Map.Entry<Integer, PriorityQueue<Integer>> entry : entrySet) {
PriorityQueue<Integer> queue = entry.getValue();
if (queue.peek() < 3) {
return false;
}
}
return true;
}
}
方法二:贪心
class Solution {
public boolean isPossible(int[] nums) {
Map<Integer, Integer> countMap = new HashMap<Integer, Integer>();
Map<Integer, Integer> endMap = new HashMap<Integer, Integer>();
for (int x : nums) {
int count = countMap.getOrDefault(x, 0) + 1;
countMap.put(x, count);
}
for (int x : nums) {
int count = countMap.getOrDefault(x, 0);
if (count > 0) {
int prevEndCount = endMap.getOrDefault(x - 1, 0);
if (prevEndCount > 0) {
countMap.put(x, count - 1);
endMap.put(x - 1, prevEndCount - 1);
endMap.put(x, endMap.getOrDefault(x, 0) + 1);
} else {
int count1 = countMap.getOrDefault(x + 1, 0);
int count2 = countMap.getOrDefault(x + 2, 0);
if (count1 > 0 && count2 > 0) {
countMap.put(x, count - 1);
countMap.put(x + 1, count1 - 1);
countMap.put(x + 2, count2 - 1);
endMap.put(x + 2, endMap.getOrDefault(x + 2, 0) + 1);
} else {
return false;
}
}
}
}
return true;
}
}