Skip to content

Commit 40329ab

Browse files
committed
feat: add rust solution to lc problem: No.3542
1 parent 6ad9c88 commit 40329ab

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

solution/3500-3599/3542.Minimum Operations to Convert All Elements to Zero/README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,18 @@ tags:
9696

9797
<!-- solution:start -->
9898

99-
### 方法一
99+
### 方法一:单调栈
100+
101+
根据题意,我们应该把数字中最小的数先变成 $0$,再把次小的数变成 $0$,依此类推。在这里过程中,如果两个数之间有更小的数隔开,那么它们需要额外的一次操作才能变成 $0$。
102+
103+
我们可以维护一个从栈底到栈顶单调递增的栈 $\textit{stk}$,遍历数组 $\textit{nums}$ 中的每个数 $\textit{x}$:
104+
105+
- 当栈顶元素大于 $\textit{x}$ 时,说明 $\textit{x}$ 将栈顶元素隔开了,我们需要把栈顶元素弹出,并将答案加 $1$,直到栈顶元素不大于 $\textit{x}$ 为止。
106+
- 如果 $\textit{x}$ 不为 $0$,且栈为空或者栈顶元素不等于 $\textit{x}$,则将 $\textit{x}$ 入栈。
107+
108+
遍历结束后,栈中剩余的元素都需要额外的一次操作才能变成 $0$,因此我们将答案加上栈的大小即可。
109+
110+
时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。
100111

101112
<!-- tabs:start -->
102113

@@ -202,6 +213,32 @@ function minOperations(nums: number[]): number {
202213
}
203214
```
204215

216+
#### Rust
217+
218+
```rust
219+
impl Solution {
220+
pub fn min_operations(nums: Vec<i32>) -> i32 {
221+
let mut stk = Vec::new();
222+
let mut ans = 0;
223+
for &x in nums.iter() {
224+
while let Some(&last) = stk.last() {
225+
if last > x {
226+
ans += 1;
227+
stk.pop();
228+
} else {
229+
break;
230+
}
231+
}
232+
if x != 0 && (stk.is_empty() || *stk.last().unwrap() != x) {
233+
stk.push(x);
234+
}
235+
}
236+
ans += stk.len() as i32;
237+
ans
238+
}
239+
}
240+
```
241+
205242
<!-- tabs:end -->
206243

207244
<!-- solution:end -->

solution/3500-3599/3542.Minimum Operations to Convert All Elements to Zero/README_EN.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,18 @@ tags:
9393

9494
<!-- solution:start -->
9595

96-
### Solution 1
96+
### Solution 1: Monotonic Stack
97+
98+
According to the problem description, we should first convert the smallest numbers to $0$, then the second smallest numbers to $0$, and so on. During this process, if two numbers are separated by smaller numbers, they require an additional operation to become $0$.
99+
100+
We can maintain a monotonically increasing stack $\textit{stk}$ from bottom to top, and traverse each number $\textit{x}$ in the array $\textit{nums}$:
101+
102+
- When the top element of the stack is greater than $\textit{x}$, it means $\textit{x}$ separates the top element. We need to pop the top element and increment the answer by $1$, continuing until the top element is not greater than $\textit{x}$.
103+
- If $\textit{x}$ is not $0$, and the stack is empty or the top element is not equal to $\textit{x}$, then push $\textit{x}$ onto the stack.
104+
105+
After traversal, the remaining elements in the stack all require an additional operation to become $0$, so we add the size of the stack to the answer.
106+
107+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$.
97108

98109
<!-- tabs:start -->
99110

@@ -199,6 +210,32 @@ function minOperations(nums: number[]): number {
199210
}
200211
```
201212

213+
#### Rust
214+
215+
```rust
216+
impl Solution {
217+
pub fn min_operations(nums: Vec<i32>) -> i32 {
218+
let mut stk = Vec::new();
219+
let mut ans = 0;
220+
for &x in nums.iter() {
221+
while let Some(&last) = stk.last() {
222+
if last > x {
223+
ans += 1;
224+
stk.pop();
225+
} else {
226+
break;
227+
}
228+
}
229+
if x != 0 && (stk.is_empty() || *stk.last().unwrap() != x) {
230+
stk.push(x);
231+
}
232+
}
233+
ans += stk.len() as i32;
234+
ans
235+
}
236+
}
237+
```
238+
202239
<!-- tabs:end -->
203240

204241
<!-- solution:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn min_operations(nums: Vec<i32>) -> i32 {
3+
let mut stk = Vec::new();
4+
let mut ans = 0;
5+
for &x in nums.iter() {
6+
while let Some(&last) = stk.last() {
7+
if last > x {
8+
ans += 1;
9+
stk.pop();
10+
} else {
11+
break;
12+
}
13+
}
14+
if x != 0 && (stk.is_empty() || *stk.last().unwrap() != x) {
15+
stk.push(x);
16+
}
17+
}
18+
ans += stk.len() as i32;
19+
ans
20+
}
21+
}

0 commit comments

Comments
 (0)