File tree 2 files changed +42
-1
lines changed
2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,083 LeetCode solutions in JavaScript
1
+ # 1,084 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcode.com/ ] ( https://leetcode.com/ )
4
4
815
815
1004|[ Max Consecutive Ones III] ( ./solutions/1004-max-consecutive-ones-iii.js ) |Medium|
816
816
1005|[ Maximize Sum Of Array After K Negations] ( ./solutions/1005-maximize-sum-of-array-after-k-negations.js ) |Easy|
817
817
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|
818
819
1009|[ Complement of Base 10 Integer] ( ./solutions/1009-complement-of-base-10-integer.js ) |Easy|
819
820
1010|[ Pairs of Songs With Total Durations Divisible by 60] ( ./solutions/1010-pairs-of-songs-with-total-durations-divisible-by-60.js ) |Medium|
820
821
1022|[ Sum of Root To Leaf Binary Numbers] ( ./solutions/1022-sum-of-root-to-leaf-binary-numbers.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments