Created
December 26, 2023 14:11
-
-
Save denpazakura/75c95b09c88ccd4482f74fea8bcd684f to your computer and use it in GitHub Desktop.
Hackerrank New Year Chaos problem solution
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
func minimumBribes(q: [Int]) -> Void { | |
var bribes = 0 | |
for (index, person) in q.enumerated() { | |
let originalPosition = person - 1 | |
if originalPosition - index > 2 { | |
print("Too chaotic") | |
return | |
} | |
// Count the number of bribes for the person within a sliding window | |
let start = max(0, originalPosition - 1) | |
let end = index | |
if start < end { | |
for j in start..<end { | |
if q[j] > originalPosition { | |
bribes += 1 | |
} | |
} | |
} | |
} | |
print(bribes) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment