659. Split Array into Consecutive Subsequences Medium

@problem@discussion
#Array#Hash Table#Greedy#Heap (Priority Queue)



1/**
2 * [659] Split Array into Consecutive Subsequences
3 *
4 * You are given an integer array nums that is sorted in non-decreasing order.
5 * Determine if it is possible to split nums into one or more subsequences such that both of the following conditions are true:
6 * 
7 * 	Each subsequence is a consecutive increasing sequence (i.e. each integer is exactly one more than the previous integer).
8 * 	All subsequences have a length of 3 or more.
9 * 
10 * Return true if you can split nums according to the above conditions, or false otherwise.
11 * 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 [<u>1</u>,2,<u>3</u>,4,<u>5</u>] while [1,3,2] is not).
12 *  
13 * Example 1:
14 * 
15 * Input: nums = [1,2,3,3,4,5]
16 * Output: true
17 * Explanation: nums can be split into the following subsequences:
18 * [<u>1</u>,<u>2</u>,<u>3</u>,3,4,5] --> 1, 2, 3
19 * [1,2,3,<u>3</u>,<u>4</u>,<u>5</u>] --> 3, 4, 5
20 * 
21 * Example 2:
22 * 
23 * Input: nums = [1,2,3,3,4,4,5,5]
24 * Output: true
25 * Explanation: nums can be split into the following subsequences:
26 * [<u>1</u>,<u>2</u>,<u>3</u>,3,<u>4</u>,4,<u>5</u>,5] --> 1, 2, 3, 4, 5
27 * [1,2,3,<u>3</u>,4,<u>4</u>,5,<u>5</u>] --> 3, 4, 5
28 * 
29 * Example 3:
30 * 
31 * Input: nums = [1,2,3,4,4,5]
32 * Output: false
33 * Explanation: It is impossible to split nums into consecutive increasing subsequences of length 3 or more.
34 * 
35 *  
36 * Constraints:
37 * 
38 * 	1 <= nums.length <= 10^4
39 * 	-1000 <= nums[i] <= 1000
40 * 	nums is sorted in non-decreasing order.
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/split-array-into-consecutive-subsequences/
46// discuss: https://leetcode.com/problems/split-array-into-consecutive-subsequences/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn is_possible(nums: Vec<i32>) -> bool {
52        false
53    }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_659() {
64    }
65}
66


Back
© 2025 bowen.ge All Rights Reserved.