Created
July 4, 2015 05:00
-
-
Save jmingtan/040aaf4c06929413bd01 to your computer and use it in GitHub Desktop.
Find longest increasing subsequence in a sequence of random numbers
This file contains hidden or 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
(ns interview.core) | |
(defn gen-seq | |
"Generates a random seq of numbers" | |
[n] | |
(for [_ (range n)] | |
(inc (rand-int 10)))) | |
(defn find-longest-seq | |
"Find longest increasing subsequence" | |
[s] | |
(let [lengths (reverse (range 1 (count s))) | |
subseqs (map #(find-seq s %) lengths)] | |
(first (remove nil? subseqs)))) | |
(defn find-seq | |
"Find increasing subsequence of length n" | |
[s n] | |
(first (filter #(apply > (reverse %)) (partition n 1 s)))) | |
(find-longest-seq [1 2 3 6 8 1 2 4 1 2 3 5 6 7 8]) | |
; (1 2 3 5 6 7 8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment