Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e3e1481

Browse files
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‎

Lines changed: 28 additions & 0 deletions
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‎

Lines changed: 23 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /