Clojure implementation of the longest ascending sub-order queue algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.