Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array nums that is sorted in non-decreasing order.
Determine if it is possible to split nums into one or more subsequences such that both of the following conditions are true:
3 or more.Return true if you can split nums according to the above conditions, or false otherwise.
A subsequence of an array is a new array that is formed from the original array by deleting some (can be none) of the elements without disturbing the relative positions of the remaining elements. (i.e., [1,3,5] is a subsequence of [1,2,3,4,5] while [1,3,2] is not).
Example 1:
Input: nums = [1,2,3,3,4,5] Output: true Explanation: nums can be split into the following subsequences: [1,2,3,3,4,5] --> 1, 2, 3 [1,2,3,3,4,5] --> 3, 4, 5
Example 2:
Input: nums = [1,2,3,3,4,4,5,5] Output: true Explanation: nums can be split into the following subsequences: [1,2,3,3,4,4,5,5] --> 1, 2, 3, 4, 5 [1,2,3,3,4,4,5,5] --> 3, 4, 5
Example 3:
Input: nums = [1,2,3,4,4,5] Output: false Explanation: It is impossible to split nums into consecutive increasing subsequences of length 3 or more.
Constraints:
1 <= nums.length <= 104-1000 <= nums[i] <= 1000nums is sorted in non-decreasing order.Problem summary: You are given an integer array nums that is sorted in non-decreasing order. Determine if it is possible to split nums into one or more subsequences such that both of the following conditions are true: Each subsequence is a consecutive increasing sequence (i.e. each integer is exactly one more than the previous integer). All subsequences have a length of 3 or more. Return true if you can split nums according to the above conditions, or false otherwise. A subsequence of an array is a new array that is formed from the original array by deleting some (can be none) of the elements without disturbing the relative positions of the remaining elements. (i.e., [1,3,5] is a subsequence of [1,2,3,4,5] while [1,3,2] is not).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Greedy
[1,2,3,3,4,5]
[1,2,3,3,4,4,5,5]
[1,2,3,4,4,5]
top-k-frequent-elements)divide-array-in-sets-of-k-consecutive-numbers)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #659: Split Array into Consecutive Subsequences
class Solution {
public boolean isPossible(int[] nums) {
Map<Integer, PriorityQueue<Integer>> d = new HashMap<>();
for (int v : nums) {
if (d.containsKey(v - 1)) {
var q = d.get(v - 1);
d.computeIfAbsent(v, k -> new PriorityQueue<>()).offer(q.poll() + 1);
if (q.isEmpty()) {
d.remove(v - 1);
}
} else {
d.computeIfAbsent(v, k -> new PriorityQueue<>()).offer(1);
}
}
for (var v : d.values()) {
if (v.peek() < 3) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #659: Split Array into Consecutive Subsequences
func isPossible(nums []int) bool {
d := map[int]*hp{}
for _, v := range nums {
if d[v] == nil {
d[v] = new(hp)
}
if h := d[v-1]; h != nil {
heap.Push(d[v], heap.Pop(h).(int)+1)
if h.Len() == 0 {
delete(d, v-1)
}
} else {
heap.Push(d[v], 1)
}
}
for _, q := range d {
if q.IntSlice[0] < 3 {
return false
}
}
return true
}
type hp struct{ sort.IntSlice }
func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() any {
a := h.IntSlice
v := a[len(a)-1]
h.IntSlice = a[:len(a)-1]
return v
}
# Accepted solution for LeetCode #659: Split Array into Consecutive Subsequences
class Solution:
def isPossible(self, nums: List[int]) -> bool:
d = defaultdict(list)
for v in nums:
if h := d[v - 1]:
heappush(d[v], heappop(h) + 1)
else:
heappush(d[v], 1)
return all(not v or v and v[0] > 2 for v in d.values())
// Accepted solution for LeetCode #659: Split Array into Consecutive Subsequences
struct Solution;
use std::collections::HashMap;
impl Solution {
fn is_possible(nums: Vec<i32>) -> bool {
let mut left: HashMap<i32, usize> = HashMap::new();
let mut end: HashMap<i32, usize> = HashMap::new();
for &x in &nums {
*left.entry(x).or_default() += 1;
}
for &x in &nums {
if *left.entry(x).or_default() == 0 {
continue;
}
if *end.entry(x - 1).or_default() > 0 {
*left.entry(x).or_default() -= 1;
*end.entry(x - 1).or_default() -= 1;
*end.entry(x).or_default() += 1;
continue;
}
if *left.entry(x + 1).or_default() > 0 && *left.entry(x + 2).or_default() > 0 {
*left.entry(x).or_default() -= 1;
*left.entry(x + 1).or_default() -= 1;
*left.entry(x + 2).or_default() -= 1;
*end.entry(x + 2).or_default() += 1;
continue;
}
return false;
}
true
}
}
#[test]
fn test() {
let nums = vec![1, 2, 3, 3, 4, 5];
let res = true;
assert_eq!(Solution::is_possible(nums), res);
let nums = vec![1, 2, 3, 3, 4, 4, 5, 5];
let res = true;
assert_eq!(Solution::is_possible(nums), res);
let nums = vec![1, 2, 3, 4, 4, 5];
let res = false;
assert_eq!(Solution::is_possible(nums), res);
}
// Accepted solution for LeetCode #659: Split Array into Consecutive Subsequences
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #659: Split Array into Consecutive Subsequences
// class Solution {
// public boolean isPossible(int[] nums) {
// Map<Integer, PriorityQueue<Integer>> d = new HashMap<>();
// for (int v : nums) {
// if (d.containsKey(v - 1)) {
// var q = d.get(v - 1);
// d.computeIfAbsent(v, k -> new PriorityQueue<>()).offer(q.poll() + 1);
// if (q.isEmpty()) {
// d.remove(v - 1);
// }
// } else {
// d.computeIfAbsent(v, k -> new PriorityQueue<>()).offer(1);
// }
// }
// for (var v : d.values()) {
// if (v.peek() < 3) {
// return false;
// }
// }
// return true;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.