Skip to content

Commit

Permalink
Add 53.. tough one
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkun committed Aug 21, 2024
1 parent 5396e8f commit ab34a41
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/ex/q053.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
;; https://4clojure.oxal.org/#/problem/053

(ns ex.q053)

(defn ans
"Problem 53, Longest Increasing Sub-Seq"
[coll]
(->> coll
(partition 2 1) ;; create overlapping pairs
(partition-by #(= (inc (first %)) (second %))) ;; partition by increasing pairs
(filter #(= (inc (ffirst %)) (second (first %)))) ;; drop violations
(sort-by count)
last ;; grab longest sequence
(reduce concat)
dedupe)) ;; remove overlapping elements
12 changes: 12 additions & 0 deletions test/ex/q053_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
;; https://4clojure.oxal.org/#/problem/053

(ns ex.q053-test
(:require [clojure.test :as t]
[ex.q053 :as sut]))

(t/deftest test-q53
(t/testing "Problem 53, Longest Increasing Sub-Seq"
(t/is (= [0 1 2 3] (sut/ans [1 0 1 2 3 0 4 5])))
(t/is (= [5 6] (sut/ans [5 6 1 3 2 7])))
(t/is (= [3 4 5] (sut/ans [2 3 3 4 5])))
(t/is (= [] (sut/ans [7 6 5 4])))))

0 comments on commit ab34a41

Please sign in to comment.