From 548b9d84415abf67b64d885f467ce003c12a68a7 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Tue, 19 Nov 2024 07:08:36 +0100 Subject: [PATCH] feat: add swift implementation to lcp problem: No.33 --- .../README.md" | 28 +++++++++++++++++++ .../Solution.swift" | 23 +++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 "lcp/LCP 33. \350\223\204\346\260\264/Solution.swift" diff --git "a/lcp/LCP 33. \350\223\204\346\260\264/README.md" "b/lcp/LCP 33. \350\223\204\346\260\264/README.md" index 6511af87a08b2..301d9505b50df 100644 --- "a/lcp/LCP 33. \350\223\204\346\260\264/README.md" +++ "b/lcp/LCP 33. \350\223\204\346\260\264/README.md" @@ -168,6 +168,34 @@ function storeWater(bucket: number[], vat: number[]): number { } ``` +#### Swift + +```swift +class Solution { + func storeWater(_ bucket: [Int], _ vat: [Int]) -> Int { + let maxVat = vat.max() ?? 0 + if maxVat == 0 { + return 0 + } + + let n = vat.count + var ans = Int.max + + for x in 1...maxVat { + var y = 0 + for i in 0.. 0 { + y += max(0, (vat[i] + x - 1) / x - bucket[i]) + } + } + ans = min(ans, x + y) + } + + return ans + } +} +``` + diff --git "a/lcp/LCP 33. \350\223\204\346\260\264/Solution.swift" "b/lcp/LCP 33. \350\223\204\346\260\264/Solution.swift" new file mode 100644 index 0000000000000..c1ade70414605 --- /dev/null +++ "b/lcp/LCP 33. \350\223\204\346\260\264/Solution.swift" @@ -0,0 +1,23 @@ +class Solution { + func storeWater(_ bucket: [Int], _ vat: [Int]) -> Int { + let maxVat = vat.max() ?? 0 + if maxVat == 0 { + return 0 + } + + let n = vat.count + var ans = Int.max + + for x in 1...maxVat { + var y = 0 + for i in 0.. 0 { + y += max(0, (vat[i] + x - 1) / x - bucket[i]) + } + } + ans = min(ans, x + y) + } + + return ans + } +}