Skip to content

Commit e3e1481

Browse files
authored
feat: add swift implementation to lcp problem: No.33 (#3779)
1 parent bbb63be commit e3e1481

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

lcp/LCP 33. 蓄水/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,34 @@ function storeWater(bucket: number[], vat: number[]): number {
168168
}
169169
```
170170

171+
#### Swift
172+
173+
```swift
174+
class Solution {
175+
func storeWater(_ bucket: [Int], _ vat: [Int]) -> Int {
176+
let maxVat = vat.max() ?? 0
177+
if maxVat == 0 {
178+
return 0
179+
}
180+
181+
let n = vat.count
182+
var ans = Int.max
183+
184+
for x in 1...maxVat {
185+
var y = 0
186+
for i in 0..<n {
187+
if vat[i] > 0 {
188+
y += max(0, (vat[i] + x - 1) / x - bucket[i])
189+
}
190+
}
191+
ans = min(ans, x + y)
192+
}
193+
194+
return ans
195+
}
196+
}
197+
```
198+
171199
<!-- tabs:end -->
172200

173201
<!-- solution:end -->

lcp/LCP 33. 蓄水/Solution.swift

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
func storeWater(_ bucket: [Int], _ vat: [Int]) -> Int {
3+
let maxVat = vat.max() ?? 0
4+
if maxVat == 0 {
5+
return 0
6+
}
7+
8+
let n = vat.count
9+
var ans = Int.max
10+
11+
for x in 1...maxVat {
12+
var y = 0
13+
for i in 0..<n {
14+
if vat[i] > 0 {
15+
y += max(0, (vat[i] + x - 1) / x - bucket[i])
16+
}
17+
}
18+
ans = min(ans, x + y)
19+
}
20+
21+
return ans
22+
}
23+
}

0 commit comments

Comments
 (0)