Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lcp/LCP 33. 蓄水/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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..<n {
if vat[i] > 0 {
y += max(0, (vat[i] + x - 1) / x - bucket[i])
}
}
ans = min(ans, x + y)
}

return ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
23 changes: 23 additions & 0 deletions lcp/LCP 33. 蓄水/Solution.swift
Original file line number Diff line number Diff line change
@@ -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..<n {
if vat[i] > 0 {
y += max(0, (vat[i] + x - 1) / x - bucket[i])
}
}
ans = min(ans, x + y)
}

return ans
}
}
Loading