Challenge Prit’s solution Takes 0.037915 msecs on input [0 0 0 1 0 1 0] (defn jumping-on-clouds [c] "O(n), (= n (count c)) Had to look up the algorithm after couldn't do for 1h. Was doing something more complicated and O(2^n)" """ (loop [c [[c 0]]] (if-not (seq (filter (fn [[path-left steps]] (seq path-left)) c)) (recur #(map (fn [[path-left steps]] (if (= (second path-left) 0) [(drop 2 path-left) (inc steps)])) c) ) )) (map (fn [[path-left steps]] [(if (= (second path-left) 0) [(drop 2 path-left) (inc steps)]) (if (= (first path-left) 0) [(rest path-left) (inc steps)]) ]) [[[1 0 0] 0] [[0 0 1 0] 9]] ) """ (loop [c (rest c) jumps 0] (if (seq c) (if (= (second c) 0) (recur (drop 2 c) (inc jumps)) ;; looked up drop online (recur (rest c) (inc jumps))) jumps)))
Hackerrank 3
Hackerrank 3
Hackerrank 3
Challenge Prit’s solution Takes 0.037915 msecs on input [0 0 0 1 0 1 0] (defn jumping-on-clouds [c] "O(n), (= n (count c)) Had to look up the algorithm after couldn't do for 1h. Was doing something more complicated and O(2^n)" """ (loop [c [[c 0]]] (if-not (seq (filter (fn [[path-left steps]] (seq path-left)) c)) (recur #(map (fn [[path-left steps]] (if (= (second path-left) 0) [(drop 2 path-left) (inc steps)])) c) ) )) (map (fn [[path-left steps]] [(if (= (second path-left) 0) [(drop 2 path-left) (inc steps)]) (if (= (first path-left) 0) [(rest path-left) (inc steps)]) ]) [[[1 0 0] 0] [[0 0 1 0] 9]] ) """ (loop [c (rest c) jumps 0] (if (seq c) (if (= (second c) 0) (recur (drop 2 c) (inc jumps)) ;; looked up drop online (recur (rest c) (inc jumps))) jumps)))