diff --git "a/lcp/LCP 50. \345\256\235\347\237\263\350\241\245\347\273\231/README.md" "b/lcp/LCP 50. \345\256\235\347\237\263\350\241\245\347\273\231/README.md" index 9dcc177c97925..9d9e8bf784e0f 100644 --- "a/lcp/LCP 50. \345\256\235\347\237\263\350\241\245\347\273\231/README.md" +++ "b/lcp/LCP 50. \345\256\235\347\237\263\350\241\245\347\273\231/README.md" @@ -154,6 +154,27 @@ function giveGem(gem: number[], operations: number[][]): number { } ``` +#### Swift + +```swift +class Solution { + func giveGem(_ gem: [Int], _ operations: [[Int]]) -> Int { + var gem = gem + + for op in operations { + let x = op[0], y = op[1] + let v = gem[x] / 2 + gem[y] += v + gem[x] -= v + } + + let maxGem = gem.max() ?? 0 + let minGem = gem.min() ?? 0 + return maxGem - minGem + } +} +``` + <!-- tabs:end --> <!-- solution:end --> diff --git "a/lcp/LCP 50. \345\256\235\347\237\263\350\241\245\347\273\231/Solution.swift" "b/lcp/LCP 50. \345\256\235\347\237\263\350\241\245\347\273\231/Solution.swift" new file mode 100644 index 0000000000000..bebc434bdcb70 --- /dev/null +++ "b/lcp/LCP 50. \345\256\235\347\237\263\350\241\245\347\273\231/Solution.swift" @@ -0,0 +1,16 @@ +class Solution { + func giveGem(_ gem: [Int], _ operations: [[Int]]) -> Int { + var gem = gem + + for op in operations { + let x = op[0], y = op[1] + let v = gem[x] / 2 + gem[y] += v + gem[x] -= v + } + + let maxGem = gem.max() ?? 0 + let minGem = gem.min() ?? 0 + return maxGem - minGem + } +} \ No newline at end of file