Skip to content

Instantly share code, notes, and snippets.

@vrat28
Created May 11, 2021 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/8af5081933379fce643282501db8a6c0 to your computer and use it in GitHub Desktop.
Save vrat28/8af5081933379fce643282501db8a6c0 to your computer and use it in GitHub Desktop.
Max Score from Cards(Swift: Sliding Window)
class Solution {
func maxScore(_ arr: [Int], _ k: Int) -> Int {
var windowSum = 0
for i in 0..<k {
windowSum += arr[i]
}
var i = k - 1, j = arr.count - 1
var bestSum = windowSum
while i >= 0 {
windowSum = windowSum + arr[j] - arr[i]
bestSum = max(bestSum, windowSum)
i -= 1
j -= 1
}
return bestSum
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment