Skip to content

Commit d756f80

Browse files
authoredNov 19, 2024··
feat: add swift implementation to lcp problem: No.50 (#3785)
1 parent 7eb35eb commit d756f80

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
 

‎lcp/LCP 50. 宝石补给/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,27 @@ function giveGem(gem: number[], operations: number[][]): number {
154154
}
155155
```
156156

157+
#### Swift
158+
159+
```swift
160+
class Solution {
161+
func giveGem(_ gem: [Int], _ operations: [[Int]]) -> Int {
162+
var gem = gem
163+
164+
for op in operations {
165+
let x = op[0], y = op[1]
166+
let v = gem[x] / 2
167+
gem[y] += v
168+
gem[x] -= v
169+
}
170+
171+
let maxGem = gem.max() ?? 0
172+
let minGem = gem.min() ?? 0
173+
return maxGem - minGem
174+
}
175+
}
176+
```
177+
157178
<!-- tabs:end -->
158179

159180
<!-- solution:end -->
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
func giveGem(_ gem: [Int], _ operations: [[Int]]) -> Int {
3+
var gem = gem
4+
5+
for op in operations {
6+
let x = op[0], y = op[1]
7+
let v = gem[x] / 2
8+
gem[y] += v
9+
gem[x] -= v
10+
}
11+
12+
let maxGem = gem.max() ?? 0
13+
let minGem = gem.min() ?? 0
14+
return maxGem - minGem
15+
}
16+
}

0 commit comments

Comments
 (0)
Please sign in to comment.