LeetCode 659: Split Array into Consecutive Subsequences Solution
Master LeetCode problem 659 (Split Array into Consecutive Subsequences), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
659. Split Array into Consecutive Subsequences
Problem Explanation
Explanation
To solve this problem, we can use a greedy approach. We iterate through the array nums and maintain two hash maps freq and append. The freq map stores the frequency of each number in the array, and the append map stores the number of subsequences we can append the current number to.
For each number in nums, we check if we can append it to an existing subsequence or form a new subsequence. We prioritize appending to an existing subsequence if possible. If we cannot append the current number to an existing subsequence, we check if we can form a new subsequence starting from the current number.
If at any point, we cannot satisfy the conditions of having consecutive increasing subsequences of length 3 or more, we return false.
Finally, if we successfully iterate through the entire array without violating the conditions, we return true.
Time Complexity
The time complexity of this approach is O(n), where n is the number of elements in the input array nums.
Space Complexity
The space complexity of this approach is O(n) to store the frequency and append information for each number in the array.
Solution Code
import java.util.HashMap;
import java.util.Map;
class Solution {
public boolean isPossible(int[] nums) {
Map<Integer, Integer> freq = new HashMap<>();
Map<Integer, Integer> append = new HashMap<>();
for (int num : nums) {
freq.put(num, freq.getOrDefault(num, 0) + 1);
}
for (int num : nums) {
if (freq.get(num) == 0) {
continue;
} else if (append.getOrDefault(num, 0) > 0) {
append.put(num, append.get(num) - 1);
append.put(num + 1, append.getOrDefault(num + 1, 0) + 1);
} 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);
append.put(num + 3, append.getOrDefault(num + 3, 0) + 1);
} else {
return false;
}
freq.put(num, freq.get(num) - 1);
}
return true;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 659 (Split Array into Consecutive Subsequences)?
This page provides optimized solutions for LeetCode problem 659 (Split Array into Consecutive Subsequences) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 659 (Split Array into Consecutive Subsequences)?
The time complexity for LeetCode 659 (Split Array into Consecutive Subsequences) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 659 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 659 in Java, C++, or Python.