Skip to content

Commit 8dedc33

Browse files
authored
feat: add solutions to lc problem: No.2735 (doocs#2146)
No.2735.Collecting Chocolates
1 parent 87363bc commit 8dedc33

File tree

8 files changed

+241
-87
lines changed

8 files changed

+241
-87
lines changed

solution/2700-2799/2735.Collecting Chocolates/README.md

+68-20
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,27 @@
5353

5454
**方法一:枚举**
5555

56-
我们考虑枚举操作的次数,定义 $f[i][j]$ 表示类型为 $i$ 的巧克力进行了 $j$ 次操作后的最小成本。那么有:
56+
我们考虑枚举操作的次数,定义 $f[i][j]$ 表示类型为 $i$ 的巧克力进行了 $j$ 次操作后的最小成本。
57+
58+
对于类型为 $i$ 的巧克力:
59+
60+
- 如果 $j = 0$,即没有进行操作,那么 $f[i][j] = nums[i]$;
61+
- 如果 $0 \lt j \leq n-1$,那么它的最小成本就是下标范围为 $[i,.. (i + j) \bmod n]$ 的巧克力的最小成本,即 $f[i][j] = \min\{nums[i], nums[i + 1], \cdots, nums[(i + j) \bmod n]\}$,或者可以写成 $f[i][j] = \min\{f[i][j - 1], nums[(i + j) \bmod n]\}$。
62+
- 如果 $j \ge n$,由于当 $j = n - 1$ 时,已经覆盖了所有巧克力的最小成本,如果 $j$ 继续增大,那么最小成本不会再变化,但是操作次数的增加却会导致最终的成本增加,因此,我们不需要考虑 $j \ge n$ 的情况。
63+
64+
综上,我们可以得到状态转移方程:
5765

5866
$$
5967
f[i][j] =
6068
\begin{cases}
6169
nums[i] ,& j = 0 \\
62-
\min(f[i][j - 1], nums[(i + j) \bmod n]) ,& j > 0
70+
\min(f[i][j - 1], nums[(i + j) \bmod n]) ,& 0 \lt j \leq n - 1
6371
\end{cases}
6472
$$
6573

66-
接下来,我们枚举操作的次数 $j$,其中 $j \in [0,..n-1]$,那么进行 $j$ 次操作的最小成本为:
67-
68-
$$
69-
\sum_{i=0}^{n-1} f[i][j] + j \times x
70-
$$
71-
72-
我们取所有操作次数中的最小值即可。
74+
最后,我们只需要枚举操作的次数 $j$,计算出每种操作次数下的最小成本,取最小值即可。即答案为 $\min\limits_{0 \leq j \leq n - 1} \sum\limits_{i = 0}^{n - 1} f[i][j] + x \times j$。
7375

74-
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 是数组 $nums$ 的长度。
76+
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 为数组 $nums$ 的长度。
7577

7678
<!-- tabs:start -->
7779

@@ -88,11 +90,7 @@ class Solution:
8890
f[i][0] = v
8991
for j in range(1, n):
9092
f[i][j] = min(f[i][j - 1], nums[(i + j) % n])
91-
ans = inf
92-
for j in range(n):
93-
cost = sum(f[i][j] for i in range(n)) + x * j
94-
ans = min(ans, cost)
95-
return ans
93+
return min(sum(f[i][j] for i in range(n)) + x * j for j in range(n))
9694
```
9795

9896
### **Java**
@@ -112,7 +110,7 @@ class Solution {
112110
}
113111
long ans = 1L << 60;
114112
for (int j = 0; j < n; ++j) {
115-
long cost = 1L * j * x;
113+
long cost = 1L * x * j;
116114
for (int i = 0; i < n; ++i) {
117115
cost += f[i][j];
118116
}
@@ -139,7 +137,7 @@ public:
139137
}
140138
long long ans = 1LL << 60;
141139
for (int j = 0; j < n; ++j) {
142-
long long cost = 1LL * j * x;
140+
long long cost = 1LL * x * j;
143141
for (int i = 0; i < n; ++i) {
144142
cost += f[i][j];
145143
}
@@ -156,17 +154,17 @@ public:
156154
func minCost(nums []int, x int) int64 {
157155
n := len(nums)
158156
f := make([][]int, n)
159-
for i := range f {
157+
for i, v := range nums {
160158
f[i] = make([]int, n)
161-
f[i][0] = nums[i]
159+
f[i][0] = v
162160
for j := 1; j < n; j++ {
163161
f[i][j] = min(f[i][j-1], nums[(i+j)%n])
164162
}
165163
}
166164
ans := 1 << 60
167165
for j := 0; j < n; j++ {
168166
cost := x * j
169-
for i := range nums {
167+
for i := 0; i < n; i++ {
170168
cost += f[i][j]
171169
}
172170
ans = min(ans, cost)
@@ -175,6 +173,56 @@ func minCost(nums []int, x int) int64 {
175173
}
176174
```
177175

176+
### **TypeScript**
177+
178+
```ts
179+
function minCost(nums: number[], x: number): number {
180+
const n = nums.length;
181+
const f: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
182+
for (let i = 0; i < n; ++i) {
183+
f[i][0] = nums[i];
184+
for (let j = 1; j < n; ++j) {
185+
f[i][j] = Math.min(f[i][j - 1], nums[(i + j) % n]);
186+
}
187+
}
188+
let ans = Infinity;
189+
for (let j = 0; j < n; ++j) {
190+
let cost = x * j;
191+
for (let i = 0; i < n; ++i) {
192+
cost += f[i][j];
193+
}
194+
ans = Math.min(ans, cost);
195+
}
196+
return ans;
197+
}
198+
```
199+
200+
### **Rust**
201+
202+
```rust
203+
impl Solution {
204+
pub fn min_cost(nums: Vec<i32>, x: i32) -> i64 {
205+
let n = nums.len();
206+
let mut f = vec![vec![0; n]; n];
207+
for i in 0..n {
208+
f[i][0] = nums[i];
209+
for j in 1..n {
210+
f[i][j] = f[i][j - 1].min(nums[(i + j) % n]);
211+
}
212+
}
213+
let mut ans = i64::MAX;
214+
for j in 0..n {
215+
let mut cost = (x as i64) * (j as i64);
216+
for i in 0..n {
217+
cost += f[i][j] as i64;
218+
}
219+
ans = ans.min(cost);
220+
}
221+
ans
222+
}
223+
}
224+
```
225+
178226
### **...**
179227

180228
```

solution/2700-2799/2735.Collecting Chocolates/README_EN.md

+80-10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ Thus, the total cost will become (1 + 5 + 1 + 5 + 1) = 13. We can prove that thi
4545

4646
## Solutions
4747

48+
**Solution 1: Enumeration**
49+
50+
We consider enumerating the number of operations, and define $f[i][j]$ as the minimum cost after performing $j$ operations on the chocolate of type $i$.
51+
52+
For the chocolate of type $i$:
53+
54+
- If $j = 0$, i.e., no operation is performed, then $f[i][j] = nums[i]$;
55+
- If $0 \lt j \leq n-1$, then its minimum cost is the minimum cost of the chocolates with indices in the range $[i,.. (i + j) \bmod n]$, i.e., $f[i][j] = \min\{nums[i], nums[i + 1], \cdots, nums[(i + j) \bmod n]\}$, or it can be written as $f[i][j] = \min\{f[i][j - 1], nums[(i + j) \bmod n]\}$.
56+
- If $j \ge n$, since when $j = n - 1$, the minimum cost of all covered chocolates has been obtained. If $j$ continues to increase, the minimum cost will not change, but the increase in the number of operations will lead to an increase in the final cost. Therefore, we do not need to consider the case where $j \ge n$.
57+
58+
In summary, we can get the state transition equation:
59+
60+
$$
61+
f[i][j] =
62+
\begin{cases}
63+
nums[i] ,& j = 0 \\
64+
\min(f[i][j - 1], nums[(i + j) \bmod n]) ,& 0 \lt j \leq n - 1
65+
\end{cases}
66+
$$
67+
68+
Finally, we only need to enumerate the number of operations $j$, calculate the minimum cost under each number of operations, and take the minimum value. That is, the answer is $\min\limits_{0 \leq j \leq n - 1} \sum\limits_{i = 0}^{n - 1} f[i][j] + x \times j$.
69+
70+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Where $n$ is the length of the array $nums$.
71+
4872
<!-- tabs:start -->
4973

5074
### **Python3**
@@ -58,11 +82,7 @@ class Solution:
5882
f[i][0] = v
5983
for j in range(1, n):
6084
f[i][j] = min(f[i][j - 1], nums[(i + j) % n])
61-
ans = inf
62-
for j in range(n):
63-
cost = sum(f[i][j] for i in range(n)) + x * j
64-
ans = min(ans, cost)
65-
return ans
85+
return min(sum(f[i][j] for i in range(n)) + x * j for j in range(n))
6686
```
6787

6888
### **Java**
@@ -80,7 +100,7 @@ class Solution {
80100
}
81101
long ans = 1L << 60;
82102
for (int j = 0; j < n; ++j) {
83-
long cost = 1L * j * x;
103+
long cost = 1L * x * j;
84104
for (int i = 0; i < n; ++i) {
85105
cost += f[i][j];
86106
}
@@ -107,7 +127,7 @@ public:
107127
}
108128
long long ans = 1LL << 60;
109129
for (int j = 0; j < n; ++j) {
110-
long long cost = 1LL * j * x;
130+
long long cost = 1LL * x * j;
111131
for (int i = 0; i < n; ++i) {
112132
cost += f[i][j];
113133
}
@@ -124,17 +144,17 @@ public:
124144
func minCost(nums []int, x int) int64 {
125145
n := len(nums)
126146
f := make([][]int, n)
127-
for i := range f {
147+
for i, v := range nums {
128148
f[i] = make([]int, n)
129-
f[i][0] = nums[i]
149+
f[i][0] = v
130150
for j := 1; j < n; j++ {
131151
f[i][j] = min(f[i][j-1], nums[(i+j)%n])
132152
}
133153
}
134154
ans := 1 << 60
135155
for j := 0; j < n; j++ {
136156
cost := x * j
137-
for i := range nums {
157+
for i := 0; i < n; i++ {
138158
cost += f[i][j]
139159
}
140160
ans = min(ans, cost)
@@ -143,6 +163,56 @@ func minCost(nums []int, x int) int64 {
143163
}
144164
```
145165

166+
### **TypeScript**
167+
168+
```ts
169+
function minCost(nums: number[], x: number): number {
170+
const n = nums.length;
171+
const f: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
172+
for (let i = 0; i < n; ++i) {
173+
f[i][0] = nums[i];
174+
for (let j = 1; j < n; ++j) {
175+
f[i][j] = Math.min(f[i][j - 1], nums[(i + j) % n]);
176+
}
177+
}
178+
let ans = Infinity;
179+
for (let j = 0; j < n; ++j) {
180+
let cost = x * j;
181+
for (let i = 0; i < n; ++i) {
182+
cost += f[i][j];
183+
}
184+
ans = Math.min(ans, cost);
185+
}
186+
return ans;
187+
}
188+
```
189+
190+
### **Rust**
191+
192+
```rust
193+
impl Solution {
194+
pub fn min_cost(nums: Vec<i32>, x: i32) -> i64 {
195+
let n = nums.len();
196+
let mut f = vec![vec![0; n]; n];
197+
for i in 0..n {
198+
f[i][0] = nums[i];
199+
for j in 1..n {
200+
f[i][j] = f[i][j - 1].min(nums[(i + j) % n]);
201+
}
202+
}
203+
let mut ans = i64::MAX;
204+
for j in 0..n {
205+
let mut cost = (x as i64) * (j as i64);
206+
for i in 0..n {
207+
cost += f[i][j] as i64;
208+
}
209+
ans = ans.min(cost);
210+
}
211+
ans
212+
}
213+
}
214+
```
215+
146216
### **...**
147217

148218
```
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
class Solution {
2-
public:
3-
long long minCost(vector<int>& nums, int x) {
4-
int n = nums.size();
5-
int f[n][n];
6-
for (int i = 0; i < n; ++i) {
7-
f[i][0] = nums[i];
8-
for (int j = 1; j < n; ++j) {
9-
f[i][j] = min(f[i][j - 1], nums[(i + j) % n]);
10-
}
11-
}
12-
long long ans = 1LL << 60;
13-
for (int j = 0; j < n; ++j) {
14-
long long cost = 1LL * j * x;
15-
for (int i = 0; i < n; ++i) {
16-
cost += f[i][j];
17-
}
18-
ans = min(ans, cost);
19-
}
20-
return ans;
21-
}
1+
class Solution {
2+
public:
3+
long long minCost(vector<int>& nums, int x) {
4+
int n = nums.size();
5+
int f[n][n];
6+
for (int i = 0; i < n; ++i) {
7+
f[i][0] = nums[i];
8+
for (int j = 1; j < n; ++j) {
9+
f[i][j] = min(f[i][j - 1], nums[(i + j) % n]);
10+
}
11+
}
12+
long long ans = 1LL << 60;
13+
for (int j = 0; j < n; ++j) {
14+
long long cost = 1LL * x * j;
15+
for (int i = 0; i < n; ++i) {
16+
cost += f[i][j];
17+
}
18+
ans = min(ans, cost);
19+
}
20+
return ans;
21+
}
2222
};

solution/2700-2799/2735.Collecting Chocolates/Solution.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
func minCost(nums []int, x int) int64 {
22
n := len(nums)
33
f := make([][]int, n)
4-
for i := range f {
4+
for i, v := range nums {
55
f[i] = make([]int, n)
6-
f[i][0] = nums[i]
6+
f[i][0] = v
77
for j := 1; j < n; j++ {
88
f[i][j] = min(f[i][j-1], nums[(i+j)%n])
99
}
1010
}
1111
ans := 1 << 60
1212
for j := 0; j < n; j++ {
1313
cost := x * j
14-
for i := range nums {
14+
for i := 0; i < n; i++ {
1515
cost += f[i][j]
1616
}
1717
ans = min(ans, cost)
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
class Solution {
2-
public long minCost(int[] nums, int x) {
3-
int n = nums.length;
4-
int[][] f = new int[n][n];
5-
for (int i = 0; i < n; ++i) {
6-
f[i][0] = nums[i];
7-
for (int j = 1; j < n; ++j) {
8-
f[i][j] = Math.min(f[i][j - 1], nums[(i + j) % n]);
9-
}
10-
}
11-
long ans = 1L << 60;
12-
for (int j = 0; j < n; ++j) {
13-
long cost = 1L * j * x;
14-
for (int i = 0; i < n; ++i) {
15-
cost += f[i][j];
16-
}
17-
ans = Math.min(ans, cost);
18-
}
19-
return ans;
20-
}
1+
class Solution {
2+
public long minCost(int[] nums, int x) {
3+
int n = nums.length;
4+
int[][] f = new int[n][n];
5+
for (int i = 0; i < n; ++i) {
6+
f[i][0] = nums[i];
7+
for (int j = 1; j < n; ++j) {
8+
f[i][j] = Math.min(f[i][j - 1], nums[(i + j) % n]);
9+
}
10+
}
11+
long ans = 1L << 60;
12+
for (int j = 0; j < n; ++j) {
13+
long cost = 1L * x * j;
14+
for (int i = 0; i < n; ++i) {
15+
cost += f[i][j];
16+
}
17+
ans = Math.min(ans, cost);
18+
}
19+
return ans;
20+
}
2121
}

0 commit comments

Comments
 (0)