Skip to content

Commit 846dd6e

Browse files
authored
feat: add swift implementation to lcp problem: No.40 (#3782)
1 parent ebf3904 commit 846dd6e

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

lcp/LCP 40. 心算挑战/README.md

+51
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,57 @@ function maxmiumScore(cards: number[], cnt: number): number {
235235
}
236236
```
237237

238+
#### Swift
239+
240+
```swift
241+
class Solution {
242+
func maximumScore(_ cards: [Int], _ cnt: Int) -> Int {
243+
let sortedCards = cards.sorted()
244+
let n = sortedCards.count
245+
var ans = 0
246+
247+
for i in 0..<cnt {
248+
ans += sortedCards[n - i - 1]
249+
}
250+
251+
if ans % 2 == 0 {
252+
return ans
253+
}
254+
255+
var smallestOddInside = Int.max
256+
var smallestEvenInside = Int.max
257+
var largestOddOutside = Int.min
258+
var largestEvenOutside = Int.min
259+
260+
for i in (n - cnt)..<n {
261+
if sortedCards[i] % 2 == 1 {
262+
smallestOddInside = min(smallestOddInside, sortedCards[i])
263+
} else {
264+
smallestEvenInside = min(smallestEvenInside, sortedCards[i])
265+
}
266+
}
267+
268+
for i in 0..<(n - cnt) {
269+
if sortedCards[i] % 2 == 1 {
270+
largestOddOutside = max(largestOddOutside, sortedCards[i])
271+
} else {
272+
largestEvenOutside = max(largestEvenOutside, sortedCards[i])
273+
}
274+
}
275+
276+
var maxScore = -1
277+
if smallestOddInside != Int.max && largestEvenOutside != Int.min {
278+
maxScore = max(maxScore, ans - smallestOddInside + largestEvenOutside)
279+
}
280+
if smallestEvenInside != Int.max && largestOddOutside != Int.min {
281+
maxScore = max(maxScore, ans - smallestEvenInside + largestOddOutside)
282+
}
283+
284+
return maxScore >= 0 ? maxScore : 0
285+
}
286+
}
287+
```
288+
238289
<!-- tabs:end -->
239290

240291
<!-- solution:end -->
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution {
2+
func maximumScore(_ cards: [Int], _ cnt: Int) -> Int {
3+
let sortedCards = cards.sorted()
4+
let n = sortedCards.count
5+
var ans = 0
6+
7+
for i in 0..<cnt {
8+
ans += sortedCards[n - i - 1]
9+
}
10+
11+
if ans % 2 == 0 {
12+
return ans
13+
}
14+
15+
var smallestOddInside = Int.max
16+
var smallestEvenInside = Int.max
17+
var largestOddOutside = Int.min
18+
var largestEvenOutside = Int.min
19+
20+
for i in (n - cnt)..<n {
21+
if sortedCards[i] % 2 == 1 {
22+
smallestOddInside = min(smallestOddInside, sortedCards[i])
23+
} else {
24+
smallestEvenInside = min(smallestEvenInside, sortedCards[i])
25+
}
26+
}
27+
28+
for i in 0..<(n - cnt) {
29+
if sortedCards[i] % 2 == 1 {
30+
largestOddOutside = max(largestOddOutside, sortedCards[i])
31+
} else {
32+
largestEvenOutside = max(largestEvenOutside, sortedCards[i])
33+
}
34+
}
35+
36+
var maxScore = -1
37+
if smallestOddInside != Int.max && largestEvenOutside != Int.min {
38+
maxScore = max(maxScore, ans - smallestOddInside + largestEvenOutside)
39+
}
40+
if smallestEvenInside != Int.max && largestOddOutside != Int.min {
41+
maxScore = max(maxScore, ans - smallestEvenInside + largestOddOutside)
42+
}
43+
44+
return maxScore >= 0 ? maxScore : 0
45+
}
46+
}

0 commit comments

Comments
 (0)