Created
December 24, 2015 09:53
-
-
Save onemouth/0677e05a6a1f29f8d02a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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