Skip to content

Commit 74838a6

Browse files
committed
add solution for leetcode problem 1423 in Kotlin
1 parent ebebd81 commit 74838a6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
fun maxScore(cardPoints: IntArray, k: Int): Int {
3+
if (cardPoints.size == k) return cardPoints.sum()
4+
var l = cardPoints.size - k
5+
var r = cardPoints.lastIndex
6+
7+
var maxScore = 0
8+
var currentScore = 0
9+
// the sum of the initial sub-array
10+
for (i in l..r) {
11+
maxScore += cardPoints[i]
12+
currentScore += cardPoints[i]
13+
}
14+
15+
while (true) {
16+
currentScore -= cardPoints[l++ % cardPoints.size]
17+
currentScore += cardPoints[++r % cardPoints.size]
18+
maxScore = maxOf(maxScore, currentScore)
19+
if (l % cardPoints.size == 0) break
20+
}
21+
return maxScore
22+
}
23+
}

0 commit comments

Comments
 (0)