Skip to content

Commit

Permalink
add solution for leetcode problem 1423 in Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
technophilist committed Apr 27, 2023
1 parent ebebd81 commit 74838a6
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions kotlin/1423-maximum-points-you-can-obtain-from-cards.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
fun maxScore(cardPoints: IntArray, k: Int): Int {
if (cardPoints.size == k) return cardPoints.sum()
var l = cardPoints.size - k
var r = cardPoints.lastIndex

var maxScore = 0
var currentScore = 0
// the sum of the initial sub-array
for (i in l..r) {
maxScore += cardPoints[i]
currentScore += cardPoints[i]
}

while (true) {
currentScore -= cardPoints[l++ % cardPoints.size]
currentScore += cardPoints[++r % cardPoints.size]
maxScore = maxOf(maxScore, currentScore)
if (l % cardPoints.size == 0) break
}
return maxScore
}
}

0 comments on commit 74838a6

Please sign in to comment.