659. Split Array into Consecutive Subsequences
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks if we can partition a given sorted array nums into one or more subsequences, where each subsequence is strictly increasing and consecutive (e.g., [3, 4, 5]) and has a length of at least 3.
Solution
Split Array into Consecutive Subsequences in Python
from collections import Counter
class Solution:
def isPossible(self, nums: List[int]) -> bool:
# freq counter to store the count of available numbers
freq = Counter(nums)
# tails counter to store the number of subsequences ending at a particular number
tails = Counter()
for num in nums:
# If this number is already used up, skip it.
if freq[num] == 0:
continue
freq[num] -= 1
# Option 1: Append to an existing subsequence.
# Check if there is a subsequence ending at num - 1.
if tails[num - 1] > 0:
tails[num - 1] -= 1
tails[num] += 1
# Option 2: Start a new subsequence of length 3.
# This requires num + 1 and num + 2 to be available.
elif freq[num + 1] > 0 and freq[num + 2] > 0:
freq[num + 1] -= 1
freq[num + 2] -= 1
tails[num + 2] += 1
# Option 3: Impossible to place the number.
else:
return False
return True
Split Array into Consecutive Subsequences in Java
class Solution {
public boolean isPossible(int[] nums) {
// freq map to store the count of available numbers
Map<Integer, Integer> freq = new HashMap<>();
for (int num : nums) {
freq.put(num, freq.getOrDefault(num, 0) + 1);
}
// tails map to store the number of subsequences ending at a particular number
Map<Integer, Integer> tails = new HashMap<>();
for (int num : nums) {
// If this number is already used up, skip it.
if (freq.get(num) == 0) {
continue;
}
freq.put(num, freq.get(num) - 1);
// Option 1: Append to an existing subsequence.
// Check if there is a subsequence ending at num - 1.
if (tails.getOrDefault(num - 1, 0) > 0) {
tails.put(num - 1, tails.get(num - 1) - 1);
tails.put(num, tails.getOrDefault(num, 0) + 1);
}
// Option 2: Start a new subsequence of length 3.
// This requires num + 1 and num + 2 to be available.
else if (freq.getOrDefault(num + 1, 0) > 0 && freq.getOrDefault(num + 2, 0) > 0) {
freq.put(num + 1, freq.get(num + 1) - 1);
freq.put(num + 2, freq.get(num + 2) - 1);
tails.put(num + 2, tails.getOrDefault(num + 2, 0) + 1);
}
// Option 3: Impossible to place the number.
else {
return false;
}
}
return true;
}
}
Split Array into Consecutive Subsequences in C++
class Solution {
public:
bool isPossible(vector<int>& nums) {
// freq map to store the count of available numbers
unordered_map<int, int> freq;
for (int num : nums) {
freq[num]++;
}
// tails map to store the number of subsequences ending at a particular number
unordered_map<int, int> tails;
for (int num : nums) {
// If this number is already used up, skip it.
if (freq[num] == 0) {
continue;
}
freq[num]--;
// Option 1: Append to an existing subsequence.
// Check if there is a subsequence ending at num - 1.
if (tails.count(num - 1) && tails[num - 1] > 0) {
tails[num - 1]--;
tails[num]++;
}
// Option 2: Start a new subsequence of length 3.
// This requires num + 1 and num + 2 to be available.
else if (freq.count(num + 1) && freq[num + 1] > 0 &&
freq.count(num + 2) && freq[num + 2] > 0) {
freq[num + 1]--;
freq[num + 2]--;
tails[num + 2]++;
}
// Option 3: Impossible to place the number.
else {
return false;
}
}
return true;
}
};
Split Array into Consecutive Subsequences in TypeScript
function isPossible(nums: number[]): boolean {
// freq map to store the count of available numbers
const freq = new Map<number, number>();
for (const num of nums) {
freq.set(num, (freq.get(num) || 0) + 1);
}
// tails map to store the number of subsequences ending at a particular number
const tails = new Map<number, number>();
for (const num of nums) {
// If this number is already used up, skip it.
if ((freq.get(num) || 0) === 0) {
continue;
}
freq.set(num, (freq.get(num) as number) - 1);
// Option 1: Append to an existing subsequence.
// Check if there is a subsequence ending at num - 1.
if ((tails.get(num - 1) || 0) > 0) {
tails.set(num - 1, (tails.get(num - 1) as number) - 1);
tails.set(num, (tails.get(num) || 0) + 1);
}
// Option 2: Start a new subsequence of length 3.
// This requires num + 1 and num + 2 to be available.
else if ((freq.get(num + 1) || 0) > 0 && (freq.get(num + 2) || 0) > 0) {
freq.set(num + 1, (freq.get(num + 1) as number) - 1);
freq.set(num + 2, (freq.get(num + 2) as number) - 1);
tails.set(num + 2, (tails.get(num + 2) || 0) + 1);
}
// Option 3: Impossible to place the number.
else {
return false;
}
}
return true;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.