Hackerrank Luck Balance
Challenge
Prikshet’s solution
With feedback from noisesmith from the Clojurians Slack channel.
(defn luck-balance
"Complexity: O(n), (= n (count contests))
TOC: >2h"
[k contests]
(letfn [(sum-firsts [coll]
(apply + (map first coll)))]
(let [unimportant (filter (comp zero? second) contests)
unimportant-sum (sum-firsts unimportant)
sorted-important (->> contests
(filter #(= 1 (second %)))
(sort))
k-sum (sum-firsts (take (- (count sorted-important) k) sorted-important))
remaining-sum (sum-firsts (drop (- (count sorted-important) k) sorted-important))]
(- (+ unimportant-sum
remaining-sum)
k-sum))))