Hackerrrank 7
Challenge
Prit’s solution
Tests 9-14 failing.
(defn minimum-swaps [arr]
"Complexity: O(n), (= n (count arr))
TOC: ~36m"
(loop [swap-low-index 0 arr arr num-swaps 0]
(if (< swap-low-index (count arr))
(let [unsorted-slice (drop swap-low-index arr)
lowest-remaining (apply min unsorted-slice)
swap-high-index (+ swap-low-index (.indexOf unsorted-slice lowest-remaining))]
#_(prn "low:" swap-low-index ", high:" swap-high-index ", arr:" arr ", unsorted-slice:" unsorted-slice)
(recur (inc swap-low-index)
(assoc arr
swap-high-index
(arr swap-low-index)
swap-low-index
(arr swap-high-index))
(if (< swap-low-index swap-high-index)
(inc num-swaps)
num-swaps)) ;; looked up swap with assoc online
)
num-swaps)))
(minimum-swaps [7 1 3 2 4 5 6])