Minimum Domino Rotations For Equal Row LeetCode solution in JavaScript

1
/**
2
* @param {number[]} tops
3
* @param {number[]} bottoms
4
* @return {number}
5
*/
6
var minDominoRotations = function (tops, bottoms) {
7
const swaps = Math.min(
8
minimum(tops[0], tops, bottoms),
9
minimum(tops[0], bottoms, tops),
10
minimum(bottoms[0], tops, bottoms),
11
minimum(bottoms[0], bottoms, tops)
12
);
13

14
return swaps === Infinity ? -1 : swaps;
15

16
function minimum(target, x, y, count = 0) {
17
for (let i = 0; i < x.length; i++) {
18
if (target !== x[i]) {
19
if (target !== y[i]) return Infinity;
20
count++;
21
}
22
}
23
return count;
24
}
25
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0