diff --git "a/lcp/LCP 39. \346\227\240\344\272\272\346\234\272\346\226\271\351\230\265/README.md" "b/lcp/LCP 39. \346\227\240\344\272\272\346\234\272\346\226\271\351\230\265/README.md" index 43091ae8772c9..a2ee1a5884fbe 100644 --- "a/lcp/LCP 39. \346\227\240\344\272\272\346\234\272\346\226\271\351\230\265/README.md" +++ "b/lcp/LCP 39. \346\227\240\344\272\272\346\234\272\346\226\271\351\230\265/README.md" @@ -179,6 +179,35 @@ function minimumSwitchingTimes(source: number[][], target: number[][]): number { } ``` +#### Swift + +```swift +class Solution { + func minimumSwitchingTimes(_ source: [[Int]], _ target: [[Int]]) -> Int { + var count = [Int: Int]() + + for row in source { + for num in row { + count[num, default: 0] += 1 + } + } + + for row in target { + for num in row { + count[num, default: 0] -= 1 + } + } + + var result = 0 + for value in count.values { + result += abs(value) + } + + return result / 2 + } +} +``` + diff --git "a/lcp/LCP 39. \346\227\240\344\272\272\346\234\272\346\226\271\351\230\265/Solution.swift" "b/lcp/LCP 39. \346\227\240\344\272\272\346\234\272\346\226\271\351\230\265/Solution.swift" new file mode 100644 index 0000000000000..39e7bfdbe1f6f --- /dev/null +++ "b/lcp/LCP 39. \346\227\240\344\272\272\346\234\272\346\226\271\351\230\265/Solution.swift" @@ -0,0 +1,24 @@ +class Solution { + func minimumSwitchingTimes(_ source: [[Int]], _ target: [[Int]]) -> Int { + var count = [Int: Int]() + + for row in source { + for num in row { + count[num, default: 0] += 1 + } + } + + for row in target { + for num in row { + count[num, default: 0] -= 1 + } + } + + var result = 0 + for value in count.values { + result += abs(value) + } + + return result / 2 + } +}