From 985097e09ac2a85ee2080426fc1e527394a560eb Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Tue, 19 Nov 2024 07:15:15 +0100 Subject: [PATCH 1/2] feat: add swift implementation to lcp problem: No.39 --- .../README.md" | 29 +++++++++++++++++++ .../Solution.swift" | 24 +++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 "lcp/LCP 39. \346\227\240\344\272\272\346\234\272\346\226\271\351\230\265/Solution.swift" 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..67223b0f33425 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 + } +} From fc342f487fffb7675a659557d69818f2e52282d6 Mon Sep 17 00:00:00 2001 From: klever34 Date: Tue, 19 Nov 2024 06:46:57 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- .../README.md" | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 67223b0f33425..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" @@ -185,24 +185,24 @@ function minimumSwitchingTimes(source: number[][], target: number[][]): number { 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 } }