Skip to content

Instantly share code, notes, and snippets.

@onemouth
Created December 24, 2015 09:53
Show Gist options
  • Save onemouth/0677e05a6a1f29f8d02a to your computer and use it in GitHub Desktop.
Save onemouth/0677e05a6a1f29f8d02a to your computer and use it in GitHub Desktop.
(defn decreasing-from-last [v]
(loop [start (dec (.length v))]
(let
[prev-start (dec start)]
(cond
(< prev-start 0) 0
(neg? (compare (get v prev-start) (get v start))) start
:else (recur (dec start))))))
(defn next-permutation [v]
(let [split-point (decreasing-from-last v)]
(cond
(= 0 split-point) v
:else v)))
(defn solve [input]
(let [result (next-permutation (vec input))]
(cond
(= result input) "no answer"
:else (clojure.string/join result))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment