Skip to content

Commit 65830da

Browse files
committed
+ problem 659
1 parent 732a776 commit 65830da

File tree

5 files changed

+180
-1
lines changed

5 files changed

+180
-1
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 659. Split Array into Consecutive Subsequences
2+
You are given an integer array `nums` that is **sorted in non-decreasing order**.
3+
4+
Determine if it is possible to split `nums` into **one or more subsequences** such that **both** of the following conditions are true:
5+
6+
* Each subsequence is a **consecutive increasing sequence** (i.e. each integer is **exactly one** more than the previous integer).
7+
* All subsequences have a length of `3` **or more**.
8+
9+
Return `true` *if you can split* `nums` *according to the above conditions, or* `false` *otherwise*.
10+
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 `[1,2,3,4,5]` while `[1,3,2]` is not).
12+
13+
#### Example 1:
14+
<pre>
15+
<strong>Input:</strong> nums = [1,2,3,3,4,5]
16+
<strong>Output:</strong> true
17+
<strong>Explanation:</strong> nums can be split into the following subsequences:
18+
[1,2,3,3,4,5] --> 1, 2, 3
19+
[1,2,3,3,4,5] --> 3, 4, 5
20+
</pre>
21+
22+
#### Example 2:
23+
<pre>
24+
<strong>Input:</strong> nums = [1,2,3,3,4,4,5,5]
25+
<strong>Output:</strong> true
26+
<strong>Explanation:</strong> nums can be split into the following subsequences:
27+
[1,2,3,3,4,4,5,5] --> 1, 2, 3, 4, 5
28+
[1,2,3,3,4,4,5,5] --> 3, 4, 5
29+
</pre>
30+
31+
#### Example 3:
32+
<pre>
33+
<strong>Input:</strong> nums = [1,2,3,4,4,5]
34+
<strong>Output:</strong> false
35+
<strong>Explanation:</strong> It is impossible to split nums into consecutive increasing subsequences of length 3 or more.
36+
</pre>
37+
38+
#### Constraints:
39+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
40+
* `-1000 <= nums[i] <= 1000`
41+
* `nums` is sorted in **non-decreasing** order.
42+
43+
## Solutions (Rust)
44+
45+
### 1. Solution
46+
```Rust
47+
use std::collections::HashMap;
48+
49+
impl Solution {
50+
pub fn is_possible(nums: Vec<i32>) -> bool {
51+
let mut count = HashMap::new();
52+
53+
for &num in &nums {
54+
let (a, b, c) = *count.get(&(num - 1)).unwrap_or(&(0, 0, 0));
55+
let (d, e, f) = *count.get(&num).unwrap_or(&(0, 0, 0));
56+
57+
if a > 0 {
58+
count.insert(num - 1, (a - 1, b, c));
59+
count.insert(num, (d, e + 1, f));
60+
} else if b > 0 {
61+
count.insert(num - 1, (a, b - 1, c));
62+
count.insert(num, (d, e, f + 1));
63+
} else if c > 0 {
64+
count.insert(num - 1, (a, b, c - 1));
65+
count.insert(num, (d, e, f + 1));
66+
} else {
67+
count.insert(num, (d + 1, e, f));
68+
}
69+
}
70+
71+
count.values().all(|&(a, b, _)| a | b == 0)
72+
}
73+
}
74+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# 659. 分割数组为连续子序列
2+
给你一个按 **非递减顺序** 排列的整数数组 `nums`
3+
4+
请你判断是否能在将 `nums` 分割成 **一个或多个子序列** 的同时满足下述 **两个** 条件:
5+
6+
* 每个子序列都是一个 **连续递增序列**(即,每个整数 **恰好** 比前一个整数大 **1** )。
7+
* 所有子序列的长度 **至少**`3`
8+
9+
如果可以分割 `nums` 并满足上述条件,则返回 `true` ;否则,返回 `false`
10+
11+
#### 示例 1:
12+
<pre>
13+
<strong>输入:</strong> nums = [1,2,3,3,4,5]
14+
<strong>输出:</strong> true
15+
<strong>解释:</strong> nums 可以分割成以下子序列:
16+
[1,2,3,3,4,5] --> 1, 2, 3
17+
[1,2,3,3,4,5] --> 3, 4, 5
18+
</pre>
19+
20+
#### 示例 2:
21+
<pre>
22+
<strong>输入:</strong> nums = [1,2,3,3,4,4,5,5]
23+
<strong>输出:</strong> true
24+
<strong>解释:</strong> nums 可以分割成以下子序列:
25+
[1,2,3,3,4,4,5,5] --> 1, 2, 3, 4, 5
26+
[1,2,3,3,4,4,5,5] --> 3, 4, 5
27+
</pre>
28+
29+
#### 示例 3:
30+
<pre>
31+
<strong>输入:</strong> nums = [1,2,3,4,4,5]
32+
<strong>输出:</strong> false
33+
<strong>解释:</strong> 无法将 nums 分割成长度至少为 3 的连续递增子序列。
34+
</pre>
35+
36+
#### 提示:
37+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
38+
* `-1000 <= nums[i] <= 1000`
39+
* `nums` 按非递减顺序排列
40+
41+
## 题解 (Rust)
42+
43+
### 1. 题解
44+
```Rust
45+
use std::collections::HashMap;
46+
47+
impl Solution {
48+
pub fn is_possible(nums: Vec<i32>) -> bool {
49+
let mut count = HashMap::new();
50+
51+
for &num in &nums {
52+
let (a, b, c) = *count.get(&(num - 1)).unwrap_or(&(0, 0, 0));
53+
let (d, e, f) = *count.get(&num).unwrap_or(&(0, 0, 0));
54+
55+
if a > 0 {
56+
count.insert(num - 1, (a - 1, b, c));
57+
count.insert(num, (d, e + 1, f));
58+
} else if b > 0 {
59+
count.insert(num - 1, (a, b - 1, c));
60+
count.insert(num, (d, e, f + 1));
61+
} else if c > 0 {
62+
count.insert(num - 1, (a, b, c - 1));
63+
count.insert(num, (d, e, f + 1));
64+
} else {
65+
count.insert(num, (d + 1, e, f));
66+
}
67+
}
68+
69+
count.values().all(|&(a, b, _)| a | b == 0)
70+
}
71+
}
72+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn is_possible(nums: Vec<i32>) -> bool {
5+
let mut count = HashMap::new();
6+
7+
for &num in &nums {
8+
let (a, b, c) = *count.get(&(num - 1)).unwrap_or(&(0, 0, 0));
9+
let (d, e, f) = *count.get(&num).unwrap_or(&(0, 0, 0));
10+
11+
if a > 0 {
12+
count.insert(num - 1, (a - 1, b, c));
13+
count.insert(num, (d, e + 1, f));
14+
} else if b > 0 {
15+
count.insert(num - 1, (a, b - 1, c));
16+
count.insert(num, (d, e, f + 1));
17+
} else if c > 0 {
18+
count.insert(num - 1, (a, b, c - 1));
19+
count.insert(num, (d, e, f + 1));
20+
} else {
21+
count.insert(num, (d + 1, e, f));
22+
}
23+
}
24+
25+
count.values().all(|&(a, b, _)| a | b == 0)
26+
}
27+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@
407407
[655][655l] |[Print Binary Tree][655] |![py]
408408
[657][657l] |[Robot Return to Origin][657] |![py]&nbsp;&nbsp;![rs]
409409
[658][658l] |[Find K Closest Elements][658] |![rs]
410+
[659][659l] |[Split Array into Consecutive Subsequences][659] |![rs]
410411
[661][661l] |[Image Smoother][661] |![rs]
411412
[662][662l] |[Maximum Width of Binary Tree][662] |![py]
412413
[665][665l] |[Non-decreasing Array][665] |![py]
@@ -1871,6 +1872,7 @@
18711872
[655]:Problemset/0655-Print%20Binary%20Tree/README.md#655-print-binary-tree
18721873
[657]:Problemset/0657-Robot%20Return%20to%20Origin/README.md#657-robot-return-to-origin
18731874
[658]:Problemset/0658-Find%20K%20Closest%20Elements/README.md#658-find-k-closest-elements
1875+
[659]:Problemset/0659-Split%20Array%20into%20Consecutive%20Subsequences/README.md#659-split-array-into-consecutive-subsequences
18741876
[661]:Problemset/0661-Image%20Smoother/README.md#661-image-smoother
18751877
[662]:Problemset/0662-Maximum%20Width%20of%20Binary%20Tree/README.md#662-maximum-width-of-binary-tree
18761878
[665]:Problemset/0665-Non-decreasing%20Array/README.md#665-non-decreasing-array
@@ -3333,6 +3335,7 @@
33333335
[655l]:https://leetcode.com/problems/print-binary-tree/
33343336
[657l]:https://leetcode.com/problems/robot-return-to-origin/
33353337
[658l]:https://leetcode.com/problems/find-k-closest-elements/
3338+
[659l]:https://leetcode.com/problems/split-array-into-consecutive-subsequences/
33363339
[661l]:https://leetcode.com/problems/image-smoother/
33373340
[662l]:https://leetcode.com/problems/maximum-width-of-binary-tree/
33383341
[665l]:https://leetcode.com/problems/non-decreasing-array/

README_CN.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,9 @@
407407
[655][655l] |[输出二叉树][655] |![py]
408408
[657][657l] |[机器人能否返回原点][657] |![py]&nbsp;&nbsp;![rs]
409409
[658][658l] |[找到 K 个最接近的元素][658] |![rs]
410+
[659][659l] |[分割数组为连续子序列][659] |![rs]
410411
[661][661l] |[图片平滑器][661] |![rs]
411-
[662][662l] |[二叉树最大宽度] |![py]
412+
[662][662l] |[二叉树最大宽度][662] |![py]
412413
[665][665l] |[非递减数列][665] |![py]
413414
[669][669l] |[修剪二叉搜索树][669] |![py]
414415
[670][670l] |[最大交换][670] |![rs]
@@ -1871,6 +1872,7 @@
18711872
[655]:Problemset/0655-Print%20Binary%20Tree/README_CN.md#655-输出二叉树
18721873
[657]:Problemset/0657-Robot%20Return%20to%20Origin/README_CN.md#657-机器人能否返回原点
18731874
[658]:Problemset/0658-Find%20K%20Closest%20Elements/README_CN.md#658-找到-k-个最接近的元素
1875+
[659]:Problemset/0659-Split%20Array%20into%20Consecutive%20Subsequences/README_CN.md#659-分割数组为连续子序列
18741876
[661]:Problemset/0661-Image%20Smoother/README_CN.md#661-图片平滑器
18751877
[662]:Problemset/0662-Maximum%20Width%20of%20Binary%20Tree/README_CN.md#662-二叉树最大宽度
18761878
[665]:Problemset/0665-Non-decreasing%20Array/README_CN.md#665-非递减数列
@@ -3333,6 +3335,7 @@
33333335
[655l]:https://leetcode.cn/problems/print-binary-tree/
33343336
[657l]:https://leetcode.cn/problems/robot-return-to-origin/
33353337
[658l]:https://leetcode.cn/problems/find-k-closest-elements/
3338+
[659l]:https://leetcode.cn/problems/split-array-into-consecutive-subsequences/
33363339
[661l]:https://leetcode.cn/problems/image-smoother/
33373340
[662l]:https://leetcode.cn/problems/maximum-width-of-binary-tree/
33383341
[665l]:https://leetcode.cn/problems/non-decreasing-array/

0 commit comments

Comments
 (0)