Skip to content

Commit dae6bc1

Browse files
committed
一刷1033
1 parent 28f356d commit dae6bc1

File tree

5 files changed

+97
-57
lines changed

5 files changed

+97
-57
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7256,14 +7256,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
72567256
//|{doc_base_url}/1032-stream-of-characters.adoc[题解]
72577257
//|Hard
72587258
//|
7259-
//
7260-
//|{counter:codes}
7261-
//|{leetcode_base_url}/moving-stones-until-consecutive/[1033. Moving Stones Until Consecutive^]
7262-
//|{source_base_url}/_1033_MovingStonesUntilConsecutive.java[Java]
7263-
//|{doc_base_url}/1033-moving-stones-until-consecutive.adoc[题解]
7264-
//|Easy
7265-
//|
7266-
//
7259+
7260+
|{counter:codes}
7261+
|{leetcode_base_url}/moving-stones-until-consecutive/[1033. Moving Stones Until Consecutive^]
7262+
|{source_base_url}/_1033_MovingStonesUntilConsecutive.java[Java]
7263+
|{doc_base_url}/1033-moving-stones-until-consecutive.adoc[题解]
7264+
|Easy
7265+
|
7266+
72677267
//|{counter:codes}
72687268
//|{leetcode_base_url}/coloring-a-border/[1034. Coloring A Border^]
72697269
//|{source_base_url}/_1034_ColoringABorder.java[Java]
Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,74 @@
11
[#1033-moving-stones-until-consecutive]
2-
= 1033. Moving Stones Until Consecutive
2+
= 1033. 移动石子直到连续
33

4-
{leetcode}/problems/moving-stones-until-consecutive/[LeetCode - Moving Stones Until Consecutive^]
4+
https://leetcode.cn/problems/moving-stones-until-consecutive/[LeetCode - 1033. 移动石子直到连续 ^]
55

6-
Three stones are on a number line at positions `a`, `b`, and `c`.
6+
三枚石子放置在数轴上,位置分别为 `a``b``c`
77

8-
Each turn, you pick up a stone at an endpoint (ie., either the lowest or highest position stone), and move it to an unoccupied position between those endpoints. Formally, let's say the stones are currently at positions `x, y, z` with `x < y < z`. You pick up the stone at either position `x` or position `z`, and move that stone to an integer position `k`, with `x < k < z` and `k != y`.
8+
每一回合,你可以从两端之一拿起一枚石子(位置最大或最小),并将其放入两端之间的任一空闲位置。形式上,假设这三枚石子当前分别位于位置 `x, y, z` `x < y < z`。那么就可以从位置 `x` 或者是位置 `z` 拿起一枚石子,并将该石子移动到某一整数位置 `k` 处,其中 `x < k < z` `k != y`
99

10-
The game ends when you cannot make any more moves, ie. the stones are in consecutive positions.
10+
当你无法进行任何移动时,即,这些石子的位置连续时,游戏结束。
1111

12-
When the game ends, what is the minimum and maximum number of moves that you could have made? Return the answer as an length 2 array: `answer = [minimum_moves, maximum_moves]`
12+
要使游戏结束,你可以执行的最小和最大移动次数分别是多少? 以长度为 2 的数组形式返回答案:`answer = [minimum_moves, maximum_moves]`
1313

14-
14+
*示例 1:*
1515

16-
*Example 1:*
17-
18-
[subs="verbatim,quotes,macros"]
19-
----
20-
*Input:* a = 1, b = 2, c = 5
21-
*Output:* [1,2]
22-
*Explanation:* Move the stone from 5 to 3, or move the stone from 5 to 4 to 3.
23-
----
24-
25-
26-
*Example 2:*
27-
28-
[subs="verbatim,quotes,macros"]
29-
----
30-
*Input:* a = 4, b = 3, c = 2
31-
*Output:* [0,0]
32-
*Explanation:* We cannot make any moves.
33-
----
34-
35-
36-
*Example 3:*
37-
38-
[subs="verbatim,quotes,macros"]
39-
----
40-
*Input:* a = 3, b = 5, c = 1
41-
*Output:* [1,2]
42-
*Explanation:* Move the stone from 1 to 4; or move the stone from 1 to 2 to 4.
43-
----
44-
45-
16+
....
17+
输入:a = 1, b = 2, c = 5
18+
输出:[1, 2]
19+
解释:将石子从 5 移动到 4 再移动到 3,或者我们可以直接将石子移动到 3。
20+
....
4621

22+
*示例 2:*
4723

24+
....
25+
输入:a = 4, b = 3, c = 2
26+
输出:[0, 0]
27+
解释:我们无法进行任何移动。
28+
....
4829

49-
*Note:*
30+
*提示:*
5031

51-
52-
. `1 <= a <= 100`
53-
. `1 <= b <= 100`
54-
. `1 <= c <= 100`
32+
. `+1 <= a <= 100+`
33+
. `+1 <= b <= 100+`
34+
. `+1 <= c <= 100+`
5535
. `a != b, b != c, c != a`
5636

5737

38+
== 思路分析
5839

59-
60-
61-
62-
63-
64-
40+
最少移动步数分几种情况考虑:
6541

42+
. 当三个数字都连续是,需要移动的次数为 0
43+
. 当其中两个数字紧邻或者相隔 1 个空位时,那么只需要移动一步
44+
. 其余情况,只需要移动两步
6645

46+
最多移动步数,就看最小值和最大值直接的差值。
6747

6848
[[src-1033]]
49+
[tabs]
50+
====
51+
一刷::
52+
+
53+
--
6954
[{java_src_attr}]
7055
----
7156
include::{sourcedir}/_1033_MovingStonesUntilConsecutive.java[tag=answer]
7257
----
58+
--
59+
60+
// 二刷::
61+
// +
62+
// --
63+
// [{java_src_attr}]
64+
// ----
65+
// include::{sourcedir}/_1033_MovingStonesUntilConsecutive_2.java[tag=answer]
66+
// ----
67+
// --
68+
====
69+
70+
71+
== 参考资料
7372

73+
. https://leetcode.cn/problems/moving-stones-until-consecutive/solutions/2249064/yi-dong-shi-zi-zhi-dao-lian-xu-by-leetco-y5kb/[1033. 移动石子直到连续 - 官方题解^]
74+
. https://leetcode.cn/problems/moving-stones-until-consecutive/solutions/2250643/fen-lei-tao-lun-pythonjavacgo-by-endless-2qyo/[1033. 移动石子直到连续 - 分类讨论^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2149,7 +2149,7 @@ include::1011-capacity-to-ship-packages-within-d-days.adoc[leveloffset=+1]
21492149

21502150
// include::1032-stream-of-characters.adoc[leveloffset=+1]
21512151

2152-
// include::1033-moving-stones-until-consecutive.adoc[leveloffset=+1]
2152+
include::1033-moving-stones-until-consecutive.adoc[leveloffset=+1]
21532153

21542154
// include::1034-coloring-a-border.adoc[leveloffset=+1]
21552155

logbook/202503.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,12 @@ endif::[]
818818
|{doc_base_url}/1052-grumpy-bookstore-owner.adoc[题解]
819819
|✅ 先计算没有生气而满意的总数,再计算因为老板憋气而满意的增量用户。
820820

821+
|{counter:codes2503}
822+
|{leetcode_base_url}/moving-stones-until-consecutive/[1033. 移动石子直到连续^]
823+
|{doc_base_url}/1033-moving-stones-until-consecutive.adoc[题解]
824+
|✅ 分类讨论
825+
826+
821827
|===
822828

823829
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
public class _1033_MovingStonesUntilConsecutive {
6+
// tag::answer[]
7+
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-05-25 22:01:29
11+
*/
12+
public int[] numMovesStones(int a, int b, int c) {
13+
int[] nums = {a, b, c};
14+
Arrays.sort(nums);
15+
a = nums[0];
16+
b = nums[1];
17+
c = nums[2];
18+
int[] result = new int[2];
19+
if (c - a == 2) {
20+
result[0] = 0;
21+
} else if (b - a <= 2 || c - b <= 2) {
22+
result[0] = 1;
23+
} else {
24+
result[0] = 2;
25+
}
26+
result[1] = c - a - 2;
27+
return result;
28+
}
29+
// end::answer[]
30+
public static void main(String[] args) {
31+
new _1033_MovingStonesUntilConsecutive().numMovesStones(3, 5, 1);
32+
}
33+
}

0 commit comments

Comments
 (0)