Created
May 11, 2021 17:25
-
-
Save vrat28/8af5081933379fce643282501db8a6c0 to your computer and use it in GitHub Desktop.
Max Score from Cards(Swift: Sliding Window)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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