Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7ef9d8e

Browse files
committedJun 1, 2025·
一刷1007
1 parent 90a48a7 commit 7ef9d8e

File tree

7 files changed

+138
-37
lines changed

7 files changed

+138
-37
lines changed
 

‎README.adoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7075,13 +7075,13 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
70757075
|Medium
70767076
|
70777077

7078-
//|{counter:codes}
7079-
//|{leetcode_base_url}/minimum-domino-rotations-for-equal-row/[1007. Minimum Domino Rotations For Equal Row^]
7080-
//|{source_base_url}/_1007_MinimumDominoRotationsForEqualRow.java[Java]
7081-
//|{doc_base_url}/1007-minimum-domino-rotations-for-equal-row.adoc[题解]
7082-
//|Medium
7083-
//|
7084-
//
7078+
|{counter:codes}
7079+
|{leetcode_base_url}/minimum-domino-rotations-for-equal-row/[1007. Minimum Domino Rotations For Equal Row^]
7080+
|{source_base_url}/_1007_MinimumDominoRotationsForEqualRow.java[Java]
7081+
|{doc_base_url}/1007-minimum-domino-rotations-for-equal-row.adoc[题解]
7082+
|Medium
7083+
|
7084+
70857085
//|{counter:codes}
70867086
//|{leetcode_base_url}/construct-binary-search-tree-from-preorder-traversal/[1008. Construct Binary Search Tree from Preorder Traversal^]
70877087
//|{source_base_url}/_1008_ConstructBinarySearchTreeFromPreorderTraversal.java[Java]
Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,76 @@
11
[#1007-minimum-domino-rotations-for-equal-row]
2-
= 1007. Minimum Domino Rotations For Equal Row
2+
= 1007. 行相等的最少多米诺旋转
33

4-
{leetcode}/problems/minimum-domino-rotations-for-equal-row/[LeetCode - Minimum Domino Rotations For Equal Row^]
4+
https://leetcode.cn/problems/minimum-domino-rotations-for-equal-row/[LeetCode - 1007. 行相等的最少多米诺旋转 ^]
55

6-
In a row of dominoes, `A[i]` and `B[i]` represent the top and bottom halves of the `i`-th domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)
6+
在一排多米诺骨牌中,`tops[i]` `bottoms[i]` 分别代表第 `i` 个多米诺骨牌的上半部分和下半部分。(一个多米诺是两个从 1 到 6 的数字同列平铺形成的 —— 该平铺的每一半上都有一个数字。)
77

8-
We may rotate the `i`-th domino, so that `A[i]` and `B[i]` swap values.
8+
我们可以旋转第 `i` 张多米诺,使得 `tops[i]` `bottoms[i]` 的值交换。
99

10-
Return the minimum number of rotations so that all the values in `A` are the same, or all the values in `B` are the same.
10+
返回能使 `tops` 中所有值或者 `bottoms` 中所有值都相同的最小旋转次数。
1111

12-
If it cannot be done, return `-1`.
12+
如果无法做到,返回 `-1`.
1313

14-
14+
*示例 1:*
1515

16-
*Example 1:*
16+
image::images/1007-01.png[{image_attr}]
1717

18-
image::https://assets.leetcode.com/uploads/2019/03/08/domino.png[{image_attr}]
18+
....
19+
输入:tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2]
20+
输出:2
21+
解释:
22+
图一表示:在我们旋转之前, tops 和 bottoms 给出的多米诺牌。
23+
如果我们旋转第二个和第四个多米诺骨牌,我们可以使上面一行中的每个值都等于 2,如图二所示。
24+
....
1925

20-
[subs="verbatim,quotes,macros"]
21-
----
22-
*Input:* A = [2,1,2,4,2,2], B = [5,2,6,2,3,2]
23-
*Output:* 2
24-
*Explanation:*
25-
The first figure represents the dominoes as given by A and B: before we do any rotations.
26-
If we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.
27-
----
26+
*示例 2:*
2827

29-
*Example 2:*
28+
....
29+
输入:tops = [3,5,1,2,3], bottoms = [3,6,3,3,4]
30+
输出:-1
31+
解释: 在这种情况下,不可能旋转多米诺牌使一行的值相等。
32+
....
3033

31-
[subs="verbatim,quotes,macros"]
32-
----
33-
*Input:* A = [3,5,1,2,3], B = [3,6,3,3,4]
34-
*Output:* -1
35-
*Explanation:*
36-
In this case, it is not possible to rotate the dominoes to make one row of values equal.
37-
----
3834

39-
35+
*提示:*
4036

41-
*Note:*
37+
* `2 \<= tops.length \<= 2 * 10^4^`
38+
* `bottoms.length == tops.length`
39+
* `+1 <= tops[i], bottoms[i] <= 6+`
4240
4341
44-
. `1 <= A[i], B[i] <= 6`
45-
. `2 <= A.length == B.length <= 20000`
4642
43+
== 思路分析
4744

45+
统计每个数字的下标,然后逐个检查每个数字的下标集合是否能完整覆盖原始数字的全部下标。
46+
47+
看题解,只需要判断是否可以都变成 `tops[0]` 或者 `bottoms[0]`(要旋转成一样的数字,那么,要么是 `tops[0]`,要么是 `bottoms[0]`(要么上面调转到下面,要么下面调转到上面,两个里面,必有其一。)),这个思路更简单高效!
4848

4949

5050
[[src-1007]]
51+
[tabs]
52+
====
53+
一刷::
54+
+
55+
--
5156
[{java_src_attr}]
5257
----
5358
include::{sourcedir}/_1007_MinimumDominoRotationsForEqualRow.java[tag=answer]
5459
----
60+
--
61+
62+
// 二刷::
63+
// +
64+
// --
65+
// [{java_src_attr}]
66+
// ----
67+
// include::{sourcedir}/_1007_MinimumDominoRotationsForEqualRow_2.java[tag=answer]
68+
// ----
69+
// --
70+
====
71+
72+
73+
== 参考资料
5574

75+
. https://leetcode.cn/problems/minimum-domino-rotations-for-equal-row/solutions/3042326/du-bian-cheng-tops0-huo-zhe-bottoms0pyth-zvnj/[1007. 行相等的最少多米诺旋转 - 都变成 tops[0\] 或者 bottoms[0\]^] -- 要旋转成一样的数字,那么,要么是 `tops[0]`,要么是 `bottoms[0]`(要么上面调转到下面,要么下面调转到上面,两个里面,必有其一。)
76+
. https://leetcode.cn/problems/minimum-domino-rotations-for-equal-row/solutions/3042120/xing-xiang-deng-de-zui-shao-duo-mi-nuo-x-l31w/[1007. 行相等的最少多米诺旋转 - 官方题解^]

‎docs/images/1007-01.png

17.5 KB
Loading

‎docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2097,7 +2097,7 @@ include::0994-rotting-oranges.adoc[leveloffset=+1]
20972097

20982098
include::1006-clumsy-factorial.adoc[leveloffset=+1]
20992099

2100-
// include::1007-minimum-domino-rotations-for-equal-row.adoc[leveloffset=+1]
2100+
include::1007-minimum-domino-rotations-for-equal-row.adoc[leveloffset=+1]
21012101

21022102
// include::1008-construct-binary-search-tree-from-preorder-traversal.adoc[leveloffset=+1]
21032103

‎logbook/202503.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,12 @@ endif::[]
888888
|{doc_base_url}/1382-balance-a-binary-search-tree.adoc[题解]
889889
|✅ 二叉搜索树,先中序遍历拿到所有节点,然后递归构造平衡二叉搜索树。
890890

891+
|{counter:codes2503}
892+
|{leetcode_base_url}/minimum-domino-rotations-for-equal-row/[1007. 行相等的最少多米诺旋转^]
893+
|{doc_base_url}/1007-minimum-domino-rotations-for-equal-row.adoc[题解]
894+
|✅ 统计每个数字的下标,然后逐个检查每个数字的下标集合是否能完整覆盖原始数字的全部下标。更优解:判断是否可以都变成 `tops[0]` 或者 `bottoms[0]`
895+
896+
891897
|===
892898

893899
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
public class _1007_MinimumDominoRotationsForEqualRow {
9+
// tag::answer[]
10+
11+
/**
12+
* @author D瓜哥 · https://www.diguage.com
13+
* @since 2025-06-01 07:20:11
14+
*/
15+
public int minDominoRotations(int[] tops, int[] bottoms) {
16+
int length = tops.length;
17+
List<Integer>[] topMap = new ArrayList[7];
18+
for (int i = 1; i < topMap.length; i++) {
19+
topMap[i] = new ArrayList<>();
20+
}
21+
List<Integer>[] bottomMap = new ArrayList[7];
22+
for (int i = 1; i < bottomMap.length; i++) {
23+
bottomMap[i] = new ArrayList<>();
24+
}
25+
for (int i = 0; i < length; i++) {
26+
int top = tops[i];
27+
topMap[top].add(i);
28+
29+
int bottom = bottoms[i];
30+
bottomMap[bottom].add(i);
31+
}
32+
int result = Integer.MAX_VALUE;
33+
for (int i = 1; i < topMap.length; i++) {
34+
List<Integer> topIdx = topMap[i];
35+
List<Integer> bottomIdx = bottomMap[i];
36+
// 总数量
37+
if (topIdx.size() + bottomIdx.size() < length) {
38+
continue;
39+
}
40+
Set<Integer> set = new HashSet<>(topIdx);
41+
set.addAll(bottomIdx);
42+
// 去除重复后的数量
43+
if (set.size()<length) {
44+
continue;
45+
}
46+
boolean ok = true;
47+
for (int j = 0; j < length; j++) {
48+
if (!set.contains(j)) {
49+
ok = false;
50+
break;
51+
}
52+
}
53+
if (ok) {
54+
result = Math.min(result,
55+
Math.min(length - topIdx.size(), length - bottomIdx.size()));
56+
}
57+
}
58+
59+
return result == Integer.MAX_VALUE ? -1 : result;
60+
}
61+
// end::answer[]
62+
63+
public static void main(String[] args) {
64+
new _1007_MinimumDominoRotationsForEqualRow()
65+
.minDominoRotations(new int[]{3, 5, 1, 2, 3}, new int[]{3, 6, 3, 3, 4});
66+
}
67+
}

‎tools/d-format-problems-description.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,10 @@ sed -i 's@`+\([a-zA-Z0-9]\+ *x *[a-zA-Z0-9]\+\)+`@`\1`@g' *.adoc
4343
#`"2"`
4444
sed -i 's@`+"\([a-zA-Z0-9]\+\)"+`@`\1`@g' *.adoc
4545
sed -i 's@​@@g' *.adoc
46+
47+
# +tops[i]+
48+
sed -i 's@+\(\w*\+\[[^\]\]\)+@\1@g' *.adoc
49+
# `+-1+` 、 `+-2.7335+`
50+
sed -i 's@+\(-[0-9\.]\+\)+@\1@g' *.adoc
51+
# `2`^`31`^`+ - 1+`
52+
#sed -i 's@`2`^`31`^`+ - 1+`@`2^31^ - 1`@g' *.adoc

0 commit comments

Comments
 (0)
Please sign in to comment.