4Clojure: 4Clojure Longest ascending sub-sequence algorithm
The description is as follows:
Given a vector of integers, find the longest consecutive sub-sequence of increasing numbers. If the sub-sequences has the same length, use the one of that occurs first. An increasing sub-sequence must has a length of 2 or greater to qualify.
Cases:
[1 0 1 2 3 0 4 5] The longest ascending subsequence sequence is [0 1 2 3]
[5 6 1 3 2 7] The longest ascending subsequence sequence is [5 6]
(DEFN test [coll]; use map to hold the ascending queue for each starting element, key is meaningless and is used only to mark a different queue (loop [flag 0 Tmp-result {flag [(First coll)]} index 0]< c2/> (if (< index (-(count coll) 1) (let [nth coll index) next (Nth Coll (inc index)] (if (> Next current), if the next element is greater than the present element, and the next element is added to the current element's queue, flag is key and remains unchanged (recur flag (Assoc Tmp-result flag (CONJ ( Tmp-result flag)) (inc index) ; otherwise, the new queue begins, a new queue is created, and key is flag+1,value for the next element (Recur (inc flag) (Assoc Tmp-result (inc flag) [next])) ; filter the longest queue (let [tmp (Vals Tmp-result)] (Loop [Final ( First tmp) s (next tmp)] (if (first s) (if (>= (count first)) ( count final) ( recur (first s) (n ext s)) (recur final (next))) ; Queue Length is at least 2 (if (> (Count Final) 1) final [])))))))))
Final Result:
User=> (Test [5 6 1 3 2 7]) [5 6]user=> (Test [1 0-1 3 0]) [-1 3]user=> (test [1 0 1 2 3 0 4 5]) [0 1 2 3]
Clojure implementation of the longest ascending sub-order queue algorithm