Skip to content

Commit a6448b3

Browse files
committed
+ problem 2547
1 parent 4112a29 commit a6448b3

File tree

5 files changed

+196
-0
lines changed

5 files changed

+196
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# 2547. Minimum Cost to Split an Array
2+
You are given an integer array `nums` and an integer `k`.
3+
4+
Split the array into some number of non-empty subarrays. The **cost** of a split is the sum of the **importance value** of each subarray in the split.
5+
6+
Let `trimmed(subarray)` be the version of the subarray where all numbers which appear only once are removed.
7+
8+
* For example, `trimmed([3,1,2,4,3,4]) = [3,4,3,4]`.
9+
10+
The **importance value** of a subarray is `k + trimmed(subarray).length`.
11+
12+
* For example, if a subarray is `[1,2,3,3,3,4,4]`, then trimmed(`[1,2,3,3,3,4,4]) = [3,3,3,4,4].`The importance value of this subarray will be `k + 5`.
13+
14+
Return *the minimum possible cost of a split of* `nums`.
15+
16+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
17+
18+
#### Example 1:
19+
<pre>
20+
<strong>Input:</strong> nums = [1,2,1,2,1,3,3], k = 2
21+
<strong>Output:</strong> 8
22+
<strong>Explanation:</strong> We split nums to have two subarrays: [1,2], [1,2,1,3,3].
23+
The importance value of [1,2] is 2 + (0) = 2.
24+
The importance value of [1,2,1,3,3] is 2 + (2 + 2) = 6.
25+
The cost of the split is 2 + 6 = 8. It can be shown that this is the minimum possible cost among all the possible splits.
26+
</pre>
27+
28+
#### Example 2:
29+
<pre>
30+
<strong>Input:</strong> nums = [1,2,1,2,1], k = 2
31+
<strong>Output:</strong> 6
32+
<strong>Explanation:</strong> We split nums to have two subarrays: [1,2], [1,2,1].
33+
The importance value of [1,2] is 2 + (0) = 2.
34+
The importance value of [1,2,1] is 2 + (2) = 4.
35+
The cost of the split is 2 + 4 = 6. It can be shown that this is the minimum possible cost among all the possible splits.
36+
</pre>
37+
38+
#### Example 3:
39+
<pre>
40+
<strong>Input:</strong> nums = [1,2,1,2,1], k = 5
41+
<strong>Output:</strong> 10
42+
<strong>Explanation:</strong> We split nums to have one subarray: [1,2,1,2,1].
43+
The importance value of [1,2,1,2,1] is 5 + (3 + 2) = 10.
44+
The cost of the split is 10. It can be shown that this is the minimum possible cost among all the possible splits.
45+
</pre>
46+
47+
#### Constraints:
48+
* `1 <= nums.length <= 1000`
49+
* `0 <= nums[i] < nums.length`
50+
* <code>1 <= k <= 10<sup>9</sup></code>
51+
52+
## Solutions (Rust)
53+
54+
### 1. Solution
55+
```Rust
56+
use std::collections::HashMap;
57+
58+
impl Solution {
59+
pub fn min_cost(nums: Vec<i32>, k: i32) -> i32 {
60+
let k = k as i64;
61+
let mut dp = vec![i64::MAX; nums.len() + 1];
62+
dp[0] = 0;
63+
64+
for i in 1..dp.len() {
65+
let mut count = HashMap::new();
66+
let mut trimmed_length = 0;
67+
68+
for j in (0..i).rev() {
69+
*count.entry(nums[j]).or_insert(0) += 1;
70+
trimmed_length += match count.get(&nums[j]).unwrap() {
71+
&1 => 0,
72+
&2 => 2,
73+
_ => 1,
74+
};
75+
dp[i] = dp[i].min(dp[j] + k + trimmed_length);
76+
}
77+
}
78+
79+
*dp.last().unwrap() as i32
80+
}
81+
}
82+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# 2547. 拆分数组的最小代价
2+
给你一个整数数组 `nums` 和一个整数 `k`
3+
4+
将数组拆分成一些非空子数组。拆分的 **代价** 是每个子数组中的 **重要性** 之和。
5+
6+
`trimmed(subarray)` 作为子数组的一个特征,其中所有仅出现一次的数字将会被移除。
7+
8+
* 例如,`trimmed([3,1,2,4,3,4]) = [3,4,3,4]`
9+
10+
子数组的 **重要性** 定义为 `k + trimmed(subarray).length`
11+
12+
* 例如,如果一个子数组是 `[1,2,3,3,3,4,4]``trimmed([1,2,3,3,3,4,4]) = [3,3,3,4,4]` 。这个子数组的重要性就是 `k + 5`
13+
14+
找出并返回拆分 `nums` 的所有可行方案中的最小代价。
15+
16+
**子数组** 是数组的一个连续 **非空** 元素序列。
17+
18+
#### 示例 1:
19+
<pre>
20+
<strong>输入:</strong> nums = [1,2,1,2,1,3,3], k = 2
21+
<strong>输出:</strong> 8
22+
<strong>解释:</strong> 将 nums 拆分成两个子数组:[1,2], [1,2,1,3,3]
23+
[1,2] 的重要性是 2 + (0) = 2 。
24+
[1,2,1,3,3] 的重要性是 2 + (2 + 2) = 6 。
25+
拆分的代价是 2 + 6 = 8 ,可以证明这是所有可行的拆分方案中的最小代价。
26+
</pre>
27+
28+
#### 示例 2:
29+
<pre>
30+
<strong>输入:</strong> nums = [1,2,1,2,1], k = 2
31+
<strong>输出:</strong> 6
32+
<strong>解释:</strong> 将 nums 拆分成两个子数组:[1,2], [1,2,1] 。
33+
[1,2] 的重要性是 2 + (0) = 2 。
34+
[1,2,1] 的重要性是 2 + (2) = 4 。
35+
拆分的代价是 2 + 4 = 6 ,可以证明这是所有可行的拆分方案中的最小代价。
36+
</pre>
37+
38+
#### 示例 3:
39+
<pre>
40+
<strong>输入:</strong> nums = [1,2,1,2,1], k = 5
41+
<strong>输出:</strong> 10
42+
<strong>解释:</strong> 将 nums 拆分成一个子数组:[1,2,1,2,1].
43+
[1,2,1,2,1] 的重要性是 5 + (3 + 2) = 10 。
44+
拆分的代价是 10 ,可以证明这是所有可行的拆分方案中的最小代价。
45+
</pre>
46+
47+
#### 提示:
48+
* `1 <= nums.length <= 1000`
49+
* `0 <= nums[i] < nums.length`
50+
* <code>1 <= k <= 10<sup>9</sup></code>
51+
52+
## 题解 (Rust)
53+
54+
### 1. 题解
55+
```Rust
56+
use std::collections::HashMap;
57+
58+
impl Solution {
59+
pub fn min_cost(nums: Vec<i32>, k: i32) -> i32 {
60+
let k = k as i64;
61+
let mut dp = vec![i64::MAX; nums.len() + 1];
62+
dp[0] = 0;
63+
64+
for i in 1..dp.len() {
65+
let mut count = HashMap::new();
66+
let mut trimmed_length = 0;
67+
68+
for j in (0..i).rev() {
69+
*count.entry(nums[j]).or_insert(0) += 1;
70+
trimmed_length += match count.get(&nums[j]).unwrap() {
71+
&1 => 0,
72+
&2 => 2,
73+
_ => 1,
74+
};
75+
dp[i] = dp[i].min(dp[j] + k + trimmed_length);
76+
}
77+
}
78+
79+
*dp.last().unwrap() as i32
80+
}
81+
}
82+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn min_cost(nums: Vec<i32>, k: i32) -> i32 {
5+
let k = k as i64;
6+
let mut dp = vec![i64::MAX; nums.len() + 1];
7+
dp[0] = 0;
8+
9+
for i in 1..dp.len() {
10+
let mut count = HashMap::new();
11+
let mut trimmed_length = 0;
12+
13+
for j in (0..i).rev() {
14+
*count.entry(nums[j]).or_insert(0) += 1;
15+
trimmed_length += match count.get(&nums[j]).unwrap() {
16+
&1 => 0,
17+
&2 => 2,
18+
_ => 1,
19+
};
20+
dp[i] = dp[i].min(dp[j] + k + trimmed_length);
21+
}
22+
}
23+
24+
*dp.last().unwrap() as i32
25+
}
26+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,7 @@
15841584
[2540][2540l]|[Minimum Common Value][2540] |![rs]
15851585
[2541][2541l]|[Minimum Operations to Make Array Equal II][2541] |![rs]
15861586
[2544][2544l]|[Alternating Digit Sum][2544] |![rs]
1587+
[2547][2547l]|[Minimum Cost to Split an Array][2547] |![rs]
15871588
[2549][2549l]|[Count Distinct Numbers on Board][2549] |![rs]
15881589
[2550][2550l]|[Count Collisions of Monkeys on a Polygon][2550] |![rs]
15891590
[2553][2553l]|[Separate the Digits in an Array][2553] |![rs]
@@ -3208,6 +3209,7 @@
32083209
[2540]:Problemset/2540-Minimum%20Common%20Value/README.md#2540-minimum-common-value
32093210
[2541]:Problemset/2541-Minimum%20Operations%20to%20Make%20Array%20Equal%20II/README.md#2541-minimum-operations-to-make-array-equal-ii
32103211
[2544]:Problemset/2544-Alternating%20Digit%20Sum/README.md#2544-alternating-digit-sum
3212+
[2547]:Problemset/2547-Minimum%20Cost%20to%20Split%20an%20Array/README.md#2547-minimum-cost-to-split-an-array
32113213
[2549]:Problemset/2549-Count%20Distinct%20Numbers%20on%20Board/README.md#2549-count-distinct-numbers-on-board
32123214
[2550]:Problemset/2550-Count%20Collisions%20of%20Monkeys%20on%20a%20Polygon/README.md#2550-count-collisions-of-monkeys-on-a-polygon
32133215
[2553]:Problemset/2553-Separate%20the%20Digits%20in%20an%20Array/README.md#2553-separate-the-digits-in-an-array
@@ -4826,6 +4828,7 @@
48264828
[2540l]:https://leetcode.com/problems/minimum-common-value/
48274829
[2541l]:https://leetcode.com/problems/minimum-operations-to-make-array-equal-ii/
48284830
[2544l]:https://leetcode.com/problems/alternating-digit-sum/
4831+
[2547l]:https://leetcode.com/problems/minimum-cost-to-split-an-array/
48294832
[2549l]:https://leetcode.com/problems/count-distinct-numbers-on-board/
48304833
[2550l]:https://leetcode.com/problems/count-collisions-of-monkeys-on-a-polygon/
48314834
[2553l]:https://leetcode.com/problems/separate-the-digits-in-an-array/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,7 @@
15841584
[2540][2540l]|[最小公共值][2540] |![rs]
15851585
[2541][2541l]|[使数组中所有元素相等的最小操作数 II][2541] |![rs]
15861586
[2544][2544l]|[交替数字和][2544] |![rs]
1587+
[2547][2547l]|[拆分数组的最小代价][2547] |![rs]
15871588
[2549][2549l]|[统计桌面上的不同数字][2549] |![rs]
15881589
[2550][2550l]|[猴子碰撞的方法数][2550] |![rs]
15891590
[2553][2553l]|[分割数组中数字的数位][2553] |![rs]
@@ -3208,6 +3209,7 @@
32083209
[2540]:Problemset/2540-Minimum%20Common%20Value/README_CN.md#2540-最小公共值
32093210
[2541]:Problemset/2541-Minimum%20Operations%20to%20Make%20Array%20Equal%20II/README_CN.md#2541-使数组中所有元素相等的最小操作数-ii
32103211
[2544]:Problemset/2544-Alternating%20Digit%20Sum/README_CN.md#2544-交替数字和
3212+
[2547]:Problemset/2547-Minimum%20Cost%20to%20Split%20an%20Array/README_CN.md#2547-拆分数组的最小代价
32113213
[2549]:Problemset/2549-Count%20Distinct%20Numbers%20on%20Board/README_CN.md#2549-统计桌面上的不同数字
32123214
[2550]:Problemset/2550-Count%20Collisions%20of%20Monkeys%20on%20a%20Polygon/README_CN.md#2550-猴子碰撞的方法数
32133215
[2553]:Problemset/2553-Separate%20the%20Digits%20in%20an%20Array/README_CN.md#2553-分割数组中数字的数位
@@ -4826,6 +4828,7 @@
48264828
[2540l]:https://leetcode.cn/problems/minimum-common-value/
48274829
[2541l]:https://leetcode.cn/problems/minimum-operations-to-make-array-equal-ii/
48284830
[2544l]:https://leetcode.cn/problems/alternating-digit-sum/
4831+
[2547l]:https://leetcode.cn/problems/minimum-cost-to-split-an-array/
48294832
[2549l]:https://leetcode.cn/problems/count-distinct-numbers-on-board/
48304833
[2550l]:https://leetcode.cn/problems/count-collisions-of-monkeys-on-a-polygon/
48314834
[2553l]:https://leetcode.cn/problems/separate-the-digits-in-an-array/

0 commit comments

Comments
 (0)