Skip to content

Instantly share code, notes, and snippets.

@jmingtan
Created July 4, 2015 05:00
Show Gist options
  • Save jmingtan/040aaf4c06929413bd01 to your computer and use it in GitHub Desktop.
Save jmingtan/040aaf4c06929413bd01 to your computer and use it in GitHub Desktop.
Find longest increasing subsequence in a sequence of random numbers
(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