-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
1007 Minimum Domino Rotations For Equal Row.swift
52 lines (42 loc) 路 1.36 KB
/
1007 Minimum Domino Rotations For Equal Row.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// 1007 Minimum Domino Rotations For Equal Row.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 19/10/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/
class Solution {
/// Finds the minimum number of rotations so that all the values in `A` are
/// the same, or all the values in `B` are the same.
///
/// - Parameters:
/// - A: `A` array of dominoes.
/// - B: `B` array of dominoes.
/// - Returns: The minimum number of rotations, if it cannot be done returns -1.
///
/// - Complexity:
/// - time: O(n), where n is the length of `A` and the length of `B`.
/// - space: O(1), only constant space is used.
func minDominoRotations(_ A: [Int], _ B: [Int]) -> Int {
let ans = check(A[0], B, A)
if ans != -1 || A[0] == B[0] {
return ans
}
return check(B[0], B, A)
}
private func check(_ x: Int, _ A: [Int], _ B: [Int]) -> Int {
var ansA = 0
var ansB = 0
for i in 0..<A.count {
guard A[i] == x || B[i] == x else { return -1 }
if A[i] != x {
ansA += 1
} else if B[i] != x {
ansB += 1
}
}
return min(ansA, ansB)
}
}