-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSplitArrayIntoConsecutiveSubsequences.java
56 lines (50 loc) · 1.55 KB
/
SplitArrayIntoConsecutiveSubsequences.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.smlnskgmail.jaman.leetcodejava.medium;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
// https://leetcode.com/problems/split-array-into-consecutive-subsequences/
public class SplitArrayIntoConsecutiveSubsequences {
private final int[] input;
public SplitArrayIntoConsecutiveSubsequences(int[] input) {
this.input = input;
}
public boolean solution() {
if (input.length < 3) {
return false;
}
Map<Integer, Integer> m = new HashMap<>();
for (int num : input) {
m.put(num, m.getOrDefault(num, 0) + 1);
}
Queue<Integer> q = new PriorityQueue<>(m.keySet());
while (!q.isEmpty()) {
int min = q.peek();
int count = 0;
while (true) {
if (!m.containsKey(min)) {
if (count < 3) {
return false;
}
break;
}
m.put(min, m.get(min) - 1);
count++;
if (m.get(min) == 0) {
if (min != q.peek()) {
return false;
}
q.poll();
}
if (m.containsKey(min + 1) && m.get(min) + 1 > m.get(min + 1)) {
if (count < 3) {
return false;
}
break;
}
min += 1;
}
}
return true;
}
}