From bcd720509278d31a68af637bfcb0556064e6f943 Mon Sep 17 00:00:00 2001 From: Lanre Adedara <lanre.adedara@shara.co> Date: Tue, 19 Nov 2024 07:31:28 +0100 Subject: [PATCH] feat: add swift implementation to lcp problem: No.50 --- .../README.md" | 21 +++++++++++++++++++ .../Solution.swift" | 16 ++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 "lcp/LCP 50. \345\256\235\347\237\263\350\241\245\347\273\231/Solution.swift" 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