# LeetCode[659]Split Array into Consecutive Subsequences(Java)

6 篇文章 0 订阅

Description:

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]

Topic:
Heap(堆) Greedy(贪婪)

Heap:

堆栈是一种执行“后进先出”算法的数据结构。

堆栈就是这样一种数据结构。它是在内存中开辟一个存储区域，数据一个一个顺序地存入（也就是“压入——push”）这个区域之中。有一个地址指针总指向最后一个压入堆栈的数据所在的数据单元，存放这个地址指针的寄存器就叫做堆栈指示器。开始放入数据的单元叫做“栈底”。数据一个一个地存入，这个过程叫做“压栈”。在压栈的过程中，每有一个数据压入堆栈，就放在和前一个单元相连的后面一个单元中，堆栈指示器中的地址自动加1。读取这些数据时，按照堆栈指示器中的地址读取数据，堆栈指示器中的地址数自动减 1。这个过程叫做“弹出pop”。如此就实现了后进先出的原则。

当一二叉的每个点都大于等于（大堆）或小于等于（小堆）它的两个子，被称堆有序。

堆是一完全二叉

PriorityQueue优先队列

PriorityQueue底层实现的数据结构是堆。

Greedy贪心：

Solution:

class Solution {
HashMap<Integer, PriorityQueue<Integer>> map = new HashMap<Integer, PriorityQueue<Integer>>();
public boolean isPossible(int[] nums) {
for(int num : nums){
PriorityQueue<Integer> last = helper(num - 1);
int len = last.isEmpty()? 0 : last.poll();
PriorityQueue<Integer> current = helper(num);
current.offer(len + 1);
}
for(int key : map.keySet()){
for(int len : map.get(key)){
if(len < 3){
return false;
}
}
}
return true;
}
public PriorityQueue<Integer> helper(int num){
PriorityQueue<Integer> temp = map.get(num);
if(temp == null){
temp = new PriorityQueue<Integer>();
map.put(num, temp);
}
return temp;
}
}

1，2，3，3，4，4，5，5

 num last len current map 1 null->(0,[ ]) 0 (1, [1]) (0,[ ] ) (1, [1]) 2 (1, [1]) 1 (2, [2]) (0,[ ] ) (1, [ ])(2, [2]) 3 (2, [2]) 2 (3, [3]) (0,[ ] ) (1, [ ])(2, [ ])(3, [3]) 3 (2, [ ]) 0 (3, [1]) (0,[ ] ) (1, [ ])(2, [ ])(3, [3])(3, [1]) 4 (3, [1]) 1 (4, [2]) (0,[ ] ) (1, [ ])(2, [ ])(3, [3])(3, [ ])(4, [2]) 4 (3, [3]) 3 (4, [4]) (0,[ ] ) (1, [ ])(2, [ ])(3, [ ])(3, [ ])(4, [2])(4, [4]) 5 (4, [2]) 2 (5, [3]) (0,[ ] ) (1, [ ])(2, [ ])(3, [ ])(3, [ ])(4, [ ])(4, [4])(5, [3]) 5 (4, [4]) 4 (5, [5]) (0,[ ] ) (1, [ ])(2, [ ])(3, [ ])(3, [ ])(4, [ ])(4, [ ])(5, [3])(5, [5])

Code:

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
05-18
05-03 942
01-21 746
09-02 1406

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。