Skip to content

Commit 1af7e36

Browse files
committedMar 31, 2025
Add solution #1007
1 parent 59e4782 commit 1af7e36

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,083 LeetCode solutions in JavaScript
1+
# 1,084 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -815,6 +815,7 @@
815815
1004|[Max Consecutive Ones III](./solutions/1004-max-consecutive-ones-iii.js)|Medium|
816816
1005|[Maximize Sum Of Array After K Negations](./solutions/1005-maximize-sum-of-array-after-k-negations.js)|Easy|
817817
1006|[Clumsy Factorial](./solutions/1006-clumsy-factorial.js)|Medium|
818+
1007|[Minimum Domino Rotations For Equal Row](./solutions/1007-minimum-domino-rotations-for-equal-row.js)|Medium|
818819
1009|[Complement of Base 10 Integer](./solutions/1009-complement-of-base-10-integer.js)|Easy|
819820
1010|[Pairs of Songs With Total Durations Divisible by 60](./solutions/1010-pairs-of-songs-with-total-durations-divisible-by-60.js)|Medium|
820821
1022|[Sum of Root To Leaf Binary Numbers](./solutions/1022-sum-of-root-to-leaf-binary-numbers.js)|Easy|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 1007. Minimum Domino Rotations For Equal Row
3+
* https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/
4+
* Difficulty: Medium
5+
*
6+
* In a row of dominoes, tops[i] and bottoms[i] represent the top and bottom halves of the
7+
* ith domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)
8+
*
9+
* We may rotate the ith domino, so that tops[i] and bottoms[i] swap values.
10+
*
11+
* Return the minimum number of rotations so that all the values in tops are the same, or all
12+
* the values in bottoms are the same.
13+
*
14+
* If it cannot be done, return -1.
15+
*/
16+
17+
/**
18+
* @param {number[]} tops
19+
* @param {number[]} bottoms
20+
* @return {number}
21+
*/
22+
var minDominoRotations = function(tops, bottoms) {
23+
const topResult = countRotations(tops[0]);
24+
if (topResult !== -1) return topResult;
25+
26+
return countRotations(bottoms[0]);
27+
28+
function countRotations(target) {
29+
let topCount = 0;
30+
let bottomCount = 0;
31+
32+
for (let i = 0; i < tops.length; i++) {
33+
if (tops[i] !== target && bottoms[i] !== target) return -1;
34+
topCount += tops[i] !== target ? 1 : 0;
35+
bottomCount += bottoms[i] !== target ? 1 : 0;
36+
}
37+
38+
return Math.min(topCount, bottomCount);
39+
}
40+
};

0 commit comments

Comments
 (0)