Difficulty: Hard Topics: seqsapp
Given a vector of integers, find the longest consecutive sub-sequence of increasing numbers. If two sub-sequences have the same length, use the one that occurs first. An increasing sub-sequence must have a length of 2 or greater to qualify. test not runoop
;; Misty ;; TODO 有部分不知足題意 (defn sub-seq [in] {:pre [(vector? in)]} (letfn [(compare [prev next] (if (and (> (count next) 1) (> (count next) (count prev))) next prev))] (loop [ret [] sub [(first in)] r (rest in)] (if (seq sub) (let [a (last sub) b (first r)] (if (= (inc a) b) (recur ret (conj sub b) (rest r)) (recur (compare ret sub) (when b [b]) (rest r)))) ret))))
;; Lo (fn [v] (let [subseqs (for [i (range (count v)) j (range (+ 2 i) (inc (count v)))] (subvec v i j))] (->> subseqs (filter #(apply < %)) reverse ;have to reverse here as max-key returns the last element if count is the same (apply max-key count [])))) ;have to add in a blank vector here, in case there is no increasing subvec