Skip to content

Instantly share code, notes, and snippets.

@denpazakura
Created December 26, 2023 14:11
Show Gist options
  • Save denpazakura/75c95b09c88ccd4482f74fea8bcd684f to your computer and use it in GitHub Desktop.
Save denpazakura/75c95b09c88ccd4482f74fea8bcd684f to your computer and use it in GitHub Desktop.
Hackerrank New Year Chaos problem solution
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