Untitled
raw download clone
TEXT
views 50
,
size 514 b
func minimumBribes(q: [Int]) -> Void {

	var sorted = q
	var moves: Int = 0

	for i in 0..<sorted.count {
		let realPosition = sorted[i] - 1
		guard i != realPosition else { continue }

		if (realPosition - i) > 2 {
			print("Too chaotic")
			return
		}
		
		for j in i+1..<sorted.count {

			if sorted[i] > sorted[j] {
				let temp = sorted[i]
				sorted[i] = sorted[j]
				sorted[j] = temp
				moves += 1
			} else if i == (sorted[i]-1) {
				break
			}
			
		}

	}

	print(moves)
}
close fullscreen
Login or Register to edit or fork this paste. It's free.