Skip to content

Commit c2542db

Browse files
committed
+ problem 2571
1 parent 4f4467f commit c2542db

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# 2571. Minimum Operations to Reduce an Integer to 0
2+
You are given a positive integer `n`, you can do the following operation **any** number of times:
3+
* Add or subtract a **power** of `2` from `n`.
4+
5+
Return *the **minimum** number of operations to make* `n` *equal to* `0`.
6+
7+
A number `x` is power of `2` if <code>x == 2<sup>i</sup></code> where `i >= 0`.
8+
9+
#### Example 1:
10+
<pre>
11+
<strong>Input:</strong> n = 39
12+
<strong>Output:</strong> 3
13+
<strong>Explanation:</strong> We can do the following operations:
14+
- Add 2<sup>0</sup> = 1 to n, so now n = 40.
15+
- Subtract 2<sup>3</sup> = 8 from n, so now n = 32.
16+
- Subtract 2<sup>5</sup> = 32 from n, so now n = 0.
17+
It can be shown that 3 is the minimum number of operations we need to make n equal to 0.
18+
</pre>
19+
20+
#### Example 2:
21+
<pre>
22+
<strong>Input:</strong> n = 54
23+
<strong>Output:</strong> 3
24+
<strong>Explanation:</strong> We can do the following operations:
25+
- Add 2<sup>1</sup> = 2 to n, so now n = 56.
26+
- Add 2<sup>3</sup> = 8 to n, so now n = 64.
27+
- Subtract 2<sup>6</sup> = 64 from n, so now n = 0.
28+
So the minimum number of operations is 3.
29+
</pre>
30+
31+
#### Constraints:
32+
* <code>1 <= n <= 10<sup>5</sup></code>
33+
34+
## Solutions (Rust)
35+
36+
### 1. Solution
37+
```Rust
38+
use std::collections::VecDeque;
39+
40+
impl Solution {
41+
pub fn min_operations(n: i32) -> i32 {
42+
let m = n.ilog2() + 1;
43+
let mut deque = VecDeque::from([(n as usize, 0)]);
44+
let mut visited = vec![false; (1 << m) + 1];
45+
visited[n as usize] = true;
46+
47+
while let Some((x, steps)) = deque.pop_front() {
48+
if x == 0 {
49+
return steps;
50+
}
51+
52+
for i in 0..=m {
53+
if x + (1 << i) < visited.len() && !visited[x + (1 << i)] {
54+
deque.push_back((x + (1 << i), steps + 1));
55+
visited[x + (1 << i)] = true;
56+
}
57+
if x >= 1 << i && !visited[x - (1 << i)] {
58+
deque.push_back((x - (1 << i), steps + 1));
59+
visited[x - (1 << i)] = true;
60+
}
61+
}
62+
}
63+
64+
unreachable!()
65+
}
66+
}
67+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# 2571. 将整数减少到零需要的最少操作数
2+
给你一个正整数 `n` ,你可以执行下述操作 **任意** 次:
3+
* `n` 加上或减去 `2` 的某个 ****
4+
5+
返回使 `n` 等于 `0` 需要执行的 **最少** 操作数。
6+
7+
如果 <code>x == 2<sup>i</sup></code> 且其中 `i >= 0` ,则数字 `x``2` 的幂。
8+
9+
#### 示例 1:
10+
<pre>
11+
<strong>输入:</strong> n = 39
12+
<strong>输出:</strong> 3
13+
<strong>解释:</strong> 我们可以执行下述操作:
14+
- n 加上 2<sup>0</sup> = 1 ,得到 n = 40 。
15+
- n 减去 2<sup>3</sup> = 8 ,得到 n = 32 。
16+
- n 减去 2<sup>5</sup> = 32 ,得到 n = 0 。
17+
可以证明使 n 等于 0 需要执行的最少操作数是 3 。
18+
</pre>
19+
20+
#### 示例 2:
21+
<pre>
22+
<strong>输入:</strong> n = 54
23+
<strong>输出:</strong> 3
24+
<strong>解释:</strong> 我们可以执行下述操作:
25+
- n 加上 2<sup>1</sup> = 2 ,得到 n = 56 。
26+
- n 加上 2<sup>3</sup> = 8 ,得到 n = 64 。
27+
- n 减去 2<sup>6</sup> = 64 ,得到 n = 0 。
28+
使 n 等于 0 需要执行的最少操作数是 3 。
29+
</pre>
30+
31+
#### 提示:
32+
* <code>1 <= n <= 10<sup>5</sup></code>
33+
34+
## 题解 (Rust)
35+
36+
### 1. 题解
37+
```Rust
38+
use std::collections::VecDeque;
39+
40+
impl Solution {
41+
pub fn min_operations(n: i32) -> i32 {
42+
let m = n.ilog2() + 1;
43+
let mut deque = VecDeque::from([(n as usize, 0)]);
44+
let mut visited = vec![false; (1 << m) + 1];
45+
visited[n as usize] = true;
46+
47+
while let Some((x, steps)) = deque.pop_front() {
48+
if x == 0 {
49+
return steps;
50+
}
51+
52+
for i in 0..=m {
53+
if x + (1 << i) < visited.len() && !visited[x + (1 << i)] {
54+
deque.push_back((x + (1 << i), steps + 1));
55+
visited[x + (1 << i)] = true;
56+
}
57+
if x >= 1 << i && !visited[x - (1 << i)] {
58+
deque.push_back((x - (1 << i), steps + 1));
59+
visited[x - (1 << i)] = true;
60+
}
61+
}
62+
}
63+
64+
unreachable!()
65+
}
66+
}
67+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use std::collections::VecDeque;
2+
3+
impl Solution {
4+
pub fn min_operations(n: i32) -> i32 {
5+
let m = n.ilog2() + 1;
6+
let mut deque = VecDeque::from([(n as usize, 0)]);
7+
let mut visited = vec![false; (1 << m) + 1];
8+
visited[n as usize] = true;
9+
10+
while let Some((x, steps)) = deque.pop_front() {
11+
if x == 0 {
12+
return steps;
13+
}
14+
15+
for i in 0..=m {
16+
if x + (1 << i) < visited.len() && !visited[x + (1 << i)] {
17+
deque.push_back((x + (1 << i), steps + 1));
18+
visited[x + (1 << i)] = true;
19+
}
20+
if x >= 1 << i && !visited[x - (1 << i)] {
21+
deque.push_back((x - (1 << i), steps + 1));
22+
visited[x - (1 << i)] = true;
23+
}
24+
}
25+
}
26+
27+
unreachable!()
28+
}
29+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,7 @@
18341834
[2567][2567l]|[Minimum Score by Changing Two Elements][2567] |![rs]
18351835
[2568][2568l]|[Minimum Impossible OR][2568] |![rs]
18361836
[2570][2570l]|[Merge Two 2D Arrays by Summing Values][2570] |![rs]
1837+
[2571][2571l]|[Minimum Operations to Reduce an Integer to 0][2571] |![rs]
18371838
[2572][2572l]|[Count the Number of Square-Free Subsets][2572] |![rs]
18381839
[2574][2574l]|[Left and Right Sum Differences][2574] |![rs]
18391840
[2575][2575l]|[Find the Divisibility Array of a String][2575] |![rs]
@@ -3710,6 +3711,7 @@
37103711
[2567]:Problemset/2567-Minimum%20Score%20by%20Changing%20Two%20Elements/README.md#2567-minimum-score-by-changing-two-elements
37113712
[2568]:Problemset/2568-Minimum%20Impossible%20OR/README.md#2568-minimum-impossible-or
37123713
[2570]:Problemset/2570-Merge%20Two%202D%20Arrays%20by%20Summing%20Values/README.md#2570-merge-two-2d-arrays-by-summing-values
3714+
[2571]:Problemset/2571-Minimum%20Operations%20to%20Reduce%20an%20Integer%20to%200/README.md#2571-minimum-operations-to-reduce-an-integer-to-0
37133715
[2572]:Problemset/2572-Count%20the%20Number%20of%20Square-Free%20Subsets/README.md#2572-count-the-number-of-square-free-subsets
37143716
[2574]:Problemset/2574-Left%20and%20Right%20Sum%20Differences/README.md#2574-left-and-right-sum-differences
37153717
[2575]:Problemset/2575-Find%20the%20Divisibility%20Array%20of%20a%20String/README.md#2575-find-the-divisibility-array-of-a-string
@@ -5580,6 +5582,7 @@
55805582
[2567l]:https://leetcode.com/problems/minimum-score-by-changing-two-elements/
55815583
[2568l]:https://leetcode.com/problems/minimum-impossible-or/
55825584
[2570l]:https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values/
5585+
[2571l]:https://leetcode.com/problems/minimum-operations-to-reduce-an-integer-to-0/
55835586
[2572l]:https://leetcode.com/problems/count-the-number-of-square-free-subsets/
55845587
[2574l]:https://leetcode.com/problems/left-and-right-sum-differences/
55855588
[2575l]:https://leetcode.com/problems/find-the-divisibility-array-of-a-string/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,7 @@
18341834
[2567][2567l]|[修改两个元素的最小分数][2567] |![rs]
18351835
[2568][2568l]|[最小无法得到的或值][2568] |![rs]
18361836
[2570][2570l]|[合并两个二维数组 - 求和法][2570] |![rs]
1837+
[2571][2571l]|[将整数减少到零需要的最少操作数][2571] |![rs]
18371838
[2572][2572l]|[无平方子集计数][2572] |![rs]
18381839
[2574][2574l]|[左右元素和的差值][2574] |![rs]
18391840
[2575][2575l]|[找出字符串的可整除数组][2575] |![rs]
@@ -3710,6 +3711,7 @@
37103711
[2567]:Problemset/2567-Minimum%20Score%20by%20Changing%20Two%20Elements/README_CN.md#2567-修改两个元素的最小分数
37113712
[2568]:Problemset/2568-Minimum%20Impossible%20OR/README_CN.md#2568-最小无法得到的或值
37123713
[2570]:Problemset/2570-Merge%20Two%202D%20Arrays%20by%20Summing%20Values/README_CN.md#2570-合并两个二维数组---求和法
3714+
[2571]:Problemset/2571-Minimum%20Operations%20to%20Reduce%20an%20Integer%20to%200/README_CN.md#2571-将整数减少到零需要的最少操作数
37133715
[2572]:Problemset/2572-Count%20the%20Number%20of%20Square-Free%20Subsets/README_CN.md#2572-无平方子集计数
37143716
[2574]:Problemset/2574-Left%20and%20Right%20Sum%20Differences/README_CN.md#2574-左右元素和的差值
37153717
[2575]:Problemset/2575-Find%20the%20Divisibility%20Array%20of%20a%20String/README_CN.md#2575-找出字符串的可整除数组
@@ -5580,6 +5582,7 @@
55805582
[2567l]:https://leetcode.cn/problems/minimum-score-by-changing-two-elements/
55815583
[2568l]:https://leetcode.cn/problems/minimum-impossible-or/
55825584
[2570l]:https://leetcode.cn/problems/merge-two-2d-arrays-by-summing-values/
5585+
[2571l]:https://leetcode.cn/problems/minimum-operations-to-reduce-an-integer-to-0/
55835586
[2572l]:https://leetcode.cn/problems/count-the-number-of-square-free-subsets/
55845587
[2574l]:https://leetcode.cn/problems/left-and-right-sum-differences/
55855588
[2575l]:https://leetcode.cn/problems/find-the-divisibility-array-of-a-string/

0 commit comments

Comments
 (0)