Skip to content

Commit 5e64fc2

Browse files
committed
一刷1769
1 parent bdedf6e commit 5e64fc2

8 files changed

+121
-37
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12409,12 +12409,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
1240912409
//|Easy
1241012410
//|
1241112411

12412-
//|{counter:codes}
12413-
//|{leetcode_base_url}/minimum-number-of-operations-to-move-all-balls-to-each-box/[1769. Minimum Number of Operations to Move All Balls to Each Box^]
12414-
//|{source_base_url}/_1769_MinimumNumberOfOperationsToMoveAllBallsToEachBox.java[Java]
12415-
//|{doc_base_url}/1769-minimum-number-of-operations-to-move-all-balls-to-each-box.adoc[题解]
12416-
//|Medium
12417-
//|
12412+
|{counter:codes}
12413+
|{leetcode_base_url}/minimum-number-of-operations-to-move-all-balls-to-each-box/[1769. Minimum Number of Operations to Move All Balls to Each Box^]
12414+
|{source_base_url}/_1769_MinimumNumberOfOperationsToMoveAllBallsToEachBox.java[Java]
12415+
|{doc_base_url}/1769-minimum-number-of-operations-to-move-all-balls-to-each-box.adoc[题解]
12416+
|Medium
12417+
|
1241812418

1241912419
//|{counter:codes}
1242012420
//|{leetcode_base_url}/maximum-score-from-performing-multiplication-operations/[1770. Maximum Score from Performing Multiplication Operations^]
Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,51 @@
11
[#1769-minimum-number-of-operations-to-move-all-balls-to-each-box]
2-
= 1769. Minimum Number of Operations to Move All Balls to Each Box
2+
= 1769. 移动所有球到每个盒子所需的最小操作数
33

4-
{leetcode}/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/[LeetCode - 1769. Minimum Number of Operations to Move All Balls to Each Box ^]
4+
https://leetcode.cn/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/[LeetCode - 1769. 移动所有球到每个盒子所需的最小操作数 ^]
55

6-
You have `n` boxes. You are given a binary string `boxes` of length `n`, where `boxes[i]` is `'0'` if the `i^th^` box is *empty*, and `'1'` if it contains *one* ball.
6+
`n` 个盒子。给你一个长度为 `n` 的二进制字符串 `boxes` ,其中 `boxes[i]` 的值为 `'0'` 表示第 `i` 个盒子是 *空* 的,而 `boxes[i]` 的值为 `'1'` 表示盒子里有 *一个* 小球。
77

8-
In one operation, you can move *one* ball from a box to an adjacent box. Box `i` is adjacent to box `j` if `abs(i - j) == 1`. Note that after doing so, there may be more than one ball in some boxes.
8+
在一步操作中,你可以将 *一个* 小球从某个盒子移动到一个与之相邻的盒子中。第 `i` 个盒子和第 `j` 个盒子相邻需满足 `abs(i - j) == 1`。注意,操作执行后,某些盒子中可能会存在不止一个小球。
99

10-
Return an array `answer` of size `n`, where `answer[i]` is the *minimum* number of operations needed to move all the balls to the `i^th^` box.
10+
返回一个长度为 `n` 的数组 `answer` ,其中 `answer[i]` 是将所有小球移动到第 `i` 个盒子所需的 *最小* 操作数。
1111

12-
Each `answer[i]` is calculated considering the *initial* state of the boxes.
12+
每个 `answer[i]` 都需要根据盒子的 *初始状态* 进行计算。
1313

14-
15-
*Example 1:*
14+
*示例 1:*
1615

17-
[subs="verbatim,quotes"]
18-
----
19-
*Input:* boxes = "110"
20-
*Output:* [1,1,3]
21-
*Explanation:* The answer for each box is as follows:
22-
1) First box: you will have to move one ball from the second box to the first box in one operation.
23-
2) Second box: you will have to move one ball from the first box to the second box in one operation.
24-
3) Third box: you will have to move one ball from the first box to the third box in two operations, and move one ball from the second box to the third box in one operation.
25-
26-
----
16+
....
17+
输入:boxes = "110"
18+
输出:[1,1,3]
19+
解释:每个盒子对应的最小操作数如下:
20+
1) 第 1 个盒子:将一个小球从第 2 个盒子移动到第 1 个盒子,需要 1 步操作。
21+
2) 第 2 个盒子:将一个小球从第 1 个盒子移动到第 2 个盒子,需要 1 步操作。
22+
3) 第 3 个盒子:将一个小球从第 1 个盒子移动到第 3 个盒子,需要 2 步操作。将一个小球从第 2 个盒子移动到第 3 个盒子,需要 1 步操作。共计 3 步操作。
23+
....
2724

28-
*Example 2:*
25+
*示例 2:*
2926

30-
[subs="verbatim,quotes"]
31-
----
32-
*Input:* boxes = "001011"
33-
*Output:* [11,8,5,4,3,4]
34-
----
27+
....
28+
输入:boxes = "001011"
29+
输出:[11,8,5,4,3,4]
30+
....
3531

36-
37-
*Constraints:*
3832

33+
*提示:*
3934

4035
* `n == boxes.length`
41-
* `1 <= n <= 2000`
42-
* `boxes[i]` is either `'0'` or `'1'`.
36+
* `+1 <= n <= 2000+`
37+
* `boxes[i]` `'0'` `'1'`
4338
4439
40+
== 思路分析
4541

42+
这道题可以理解成各个 `1` 到“本地”的距离之和。最容易想到的方案是,两重循环,计算当前节点到各个 `1` 的距离之和。
4643

47-
== 思路分析
44+
更取巧的解法是利用前缀和的原理,先从左到右,统计左侧有多少个 `1`, 从左侧到当前节点,则需要从左侧的操作次数再加上左侧节点的数量,这不就是“前缀和”吗?然后,分别计算左右两侧到当前节点的距离。最后,两组距离分别分别对应相加,则可以计算书最后结果。
45+
46+
image::images/1769-10.png[{image_attr}]
47+
48+
image::images/1769-11.png[{image_attr}]
4849

4950

5051
[[src-1769]]
@@ -59,6 +60,15 @@ include::{sourcedir}/_1769_MinimumNumberOfOperationsToMoveAllBallsToEachBox.java
5960
----
6061
--
6162
63+
一刷(优化)::
64+
+
65+
--
66+
[{java_src_attr}]
67+
----
68+
include::{sourcedir}/_1769_MinimumNumberOfOperationsToMoveAllBallsToEachBox_1.java[tag=answer]
69+
----
70+
--
71+
6272
// 二刷::
6373
// +
6474
// --
@@ -72,4 +82,5 @@ include::{sourcedir}/_1769_MinimumNumberOfOperationsToMoveAllBallsToEachBox.java
7282

7383
== 参考资料
7484

75-
85+
. https://leetcode.cn/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/solutions/2000188/yi-dong-suo-you-qiu-dao-mei-ge-he-zi-suo-y50e/[1769. 移动所有球到每个盒子所需的最小操作数 - 官方题解^]
86+
. https://leetcode.cn/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/solutions/2001791/-by-muse-77-ilaz/[1769. 移动所有球到每个盒子所需的最小操作数 - 【爪哇缪斯】图解LeetCode^]

docs/images/1769-10.png

94.2 KB
Loading

docs/images/1769-11.png

461 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3622,7 +3622,7 @@ include::1650-lowest-common-ancestor-of-a-binary-tree-iii.adoc[leveloffset=+1]
36223622

36233623
// include::1768-merge-strings-alternately.adoc[leveloffset=+1]
36243624

3625-
// include::1769-minimum-number-of-operations-to-move-all-balls-to-each-box.adoc[leveloffset=+1]
3625+
include::1769-minimum-number-of-operations-to-move-all-balls-to-each-box.adoc[leveloffset=+1]
36263626

36273627
// include::1770-maximum-score-from-performing-multiplication-operations.adoc[leveloffset=+1]
36283628

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,11 @@ endif::[]
658658
|{doc_base_url}/2320-count-number-of-ways-to-place-houses.adoc[题解]
659659
|❌ 先只考虑一边的情况,再组合两边的情况。只考虑一边的情况,就是一个斐波那契数列: stem:[f(i) = f(i-1) + f(i-2)]。
660660

661+
|{counter:codes2503}
662+
|{leetcode_base_url}/minimum-number-of-operations-to-move-all-balls-to-each-box/[1769. 移动所有球到每个盒子所需的最小操作数^]
663+
|{doc_base_url}/1769-minimum-number-of-operations-to-move-all-balls-to-each-box.adoc[题解]
664+
|✅ 最简单的解法双重循环。取巧的解法是,利用“前缀和”的思路,分别计算当前节点左右两侧的操作次数,然后再相加。
665+
661666
|===
662667

663668
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _1769_MinimumNumberOfOperationsToMoveAllBallsToEachBox {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-05-11 20:51:33
9+
*/
10+
public int[] minOperations(String boxes) {
11+
int length = boxes.length();
12+
int[] result = new int[length];
13+
for (int i = 0; i < length; i++) {
14+
for (int j = 0; j < length; j++) {
15+
if (boxes.charAt(j) == '1') {
16+
result[i] += Math.abs(i - j);
17+
}
18+
}
19+
}
20+
return result;
21+
}
22+
// end::answer[]
23+
public static void main(String[] args) {
24+
new _1769_MinimumNumberOfOperationsToMoveAllBallsToEachBox()
25+
.minOperations("1010101010");
26+
}
27+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _1769_MinimumNumberOfOperationsToMoveAllBallsToEachBox_1 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-05-11 20:51:33
8+
*/
9+
public int[] minOperations(String boxes) {
10+
int length = boxes.length();
11+
int[] lop = new int[length];
12+
int left = 0;
13+
for (int i = 0; i < length; i++) {
14+
if (i > 0) {
15+
lop[i] = lop[i - 1] + left;
16+
}
17+
if (boxes.charAt(i) == '1') {
18+
left++;
19+
}
20+
}
21+
int right = 0;
22+
int[] result = new int[length];
23+
for (int i = length - 1; i >= 0; i--) {
24+
if (i < length - 1) {
25+
result[i] = result[i + 1] + right;
26+
}
27+
if (boxes.charAt(i) == '1') {
28+
right++;
29+
}
30+
}
31+
for (int i = 0; i < length; i++) {
32+
result[i] = lop[i] + result[i];
33+
}
34+
return result;
35+
}
36+
// end::answer[]
37+
public static void main(String[] args) {
38+
new _1769_MinimumNumberOfOperationsToMoveAllBallsToEachBox_1()
39+
.minOperations("1010101010");
40+
}
41+
}

0 commit comments

Comments
 (0)