From 27ca33605621b741003beb214e6c267630904590 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Mon, 27 Jan 2025 10:56:00 +0100 Subject: [PATCH] feat: add swift implementation to lcp problem: No.66 --- .../README.md" | 23 +++++++++++++++++++ .../Solution.swift" | 18 +++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 "lcp/LCP 66. \346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217/Solution.swift" diff --git "a/lcp/LCP 66. \346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217/README.md" "b/lcp/LCP 66. \346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217/README.md" index b61565afd4889..734bad71eb452 100644 --- "a/lcp/LCP 66. \346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217/README.md" +++ "b/lcp/LCP 66. \346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217/README.md" @@ -151,6 +151,29 @@ func minNumBooths(demand []string) (ans int) { } ``` +#### Swift + +```swift +class Solution { + func minNumBooths(_ demand: [String]) -> Int { + var maxBooths = [Int](repeating: 0, count: 26) + + for day in demand { + var dailyCount = [Int](repeating: 0, count: 26) + for char in day { + let index = Int(char.asciiValue! - Character("a").asciiValue!) + dailyCount[index] += 1 + } + for i in 0..<26 { + maxBooths[i] = max(maxBooths[i], dailyCount[i]) + } + } + + return maxBooths.reduce(0, +) + } +} +``` + diff --git "a/lcp/LCP 66. \346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217/Solution.swift" "b/lcp/LCP 66. \346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217/Solution.swift" new file mode 100644 index 0000000000000..ba1b0d803592d --- /dev/null +++ "b/lcp/LCP 66. \346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217/Solution.swift" @@ -0,0 +1,18 @@ +class Solution { + func minNumBooths(_ demand: [String]) -> Int { + var maxBooths = [Int](repeating: 0, count: 26) + + for day in demand { + var dailyCount = [Int](repeating: 0, count: 26) + for char in day { + let index = Int(char.asciiValue! - Character("a").asciiValue!) + dailyCount[index] += 1 + } + for i in 0..<26 { + maxBooths[i] = max(maxBooths[i], dailyCount[i]) + } + } + + return maxBooths.reduce(0, +) + } +} \ No newline at end of file