Skip to content

Commit ebf3904

Browse files
authored
feat: add swift implementation to lcp problem: No.39 (#3781)
1 parent b96891e commit ebf3904

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lcp/LCP 39. 无人机方阵/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,35 @@ function minimumSwitchingTimes(source: number[][], target: number[][]): number {
179179
}
180180
```
181181

182+
#### Swift
183+
184+
```swift
185+
class Solution {
186+
func minimumSwitchingTimes(_ source: [[Int]], _ target: [[Int]]) -> Int {
187+
var count = [Int: Int]()
188+
189+
for row in source {
190+
for num in row {
191+
count[num, default: 0] += 1
192+
}
193+
}
194+
195+
for row in target {
196+
for num in row {
197+
count[num, default: 0] -= 1
198+
}
199+
}
200+
201+
var result = 0
202+
for value in count.values {
203+
result += abs(value)
204+
}
205+
206+
return result / 2
207+
}
208+
}
209+
```
210+
182211
<!-- tabs:end -->
183212

184213
<!-- solution:end -->
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
func minimumSwitchingTimes(_ source: [[Int]], _ target: [[Int]]) -> Int {
3+
var count = [Int: Int]()
4+
5+
for row in source {
6+
for num in row {
7+
count[num, default: 0] += 1
8+
}
9+
}
10+
11+
for row in target {
12+
for num in row {
13+
count[num, default: 0] -= 1
14+
}
15+
}
16+
17+
var result = 0
18+
for value in count.values {
19+
result += abs(value)
20+
}
21+
22+
return result / 2
23+
}
24+
}

0 commit comments

Comments
 (0)