diff options
| author | Tyler Wardhaugh <tyler.wardhaugh@entercom.com> | 2020-08-26 09:03:09 -0700 |
|---|---|---|
| committer | Tyler Wardhaugh <tyler.wardhaugh@entercom.com> | 2020-08-26 14:17:11 -0700 |
| commit | bd7c995da743e11c61883e148c1fb88a5406e8d1 (patch) | |
| tree | a6b0d8b0c060e7c0dd2b626418ac11495e2d653c | |
| parent | 7d8861a1b24fa8e314b46a6aef954a90200f33d2 (diff) | |
| download | perlweeklychallenge-club-bd7c995da743e11c61883e148c1fb88a5406e8d1.tar.gz perlweeklychallenge-club-bd7c995da743e11c61883e148c1fb88a5406e8d1.tar.bz2 perlweeklychallenge-club-bd7c995da743e11c61883e148c1fb88a5406e8d1.zip | |
Task 2
3 files changed, 24 insertions, 7 deletions
diff --git a/challenge-075/tyler-wardhaugh/clojure/README.md b/challenge-075/tyler-wardhaugh/clojure/README.md index 8c07634ced..4bc8af0088 100644 --- a/challenge-075/tyler-wardhaugh/clojure/README.md +++ b/challenge-075/tyler-wardhaugh/clojure/README.md @@ -19,7 +19,7 @@ Run Task #1 with input (SUM COIN [COIN]...) Run Task #2 with input: - $ clojure -m tw.weekly.ch-2 "asdjfklsfasdjfklq" + $ clojure -m tw.weekly.ch-2 2 1 4 5 3 7 ## Project Template diff --git a/challenge-075/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj b/challenge-075/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj index a71ab930c4..72a4e05269 100644 --- a/challenge-075/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj +++ b/challenge-075/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj @@ -1,10 +1,27 @@ (ns tw.weekly.ch-2 (:require [clojure.edn :as edn])) +; algorithm source: https://stackoverflow.com/a/35931960 +; Answer by Spectral (https://stackoverflow.com/users/2558887/spectral) +; (modified from Python) + +(defn largest-rectangle-histogram + "Compute the area of the largest possible rectangle within a histogram." + [hist] + (let [decorated (into [] (concat [-1] hist [-1])) + stack (atom (list [0 (first decorated)])) + max-area (atom 0)] + (reduce-kv (fn [ans k v] + (while (< v (-> @stack peek last)) + (let [head (-> (swap-vals! stack pop) first peek last) + area (* head (- k (-> @stack peek first) 1))] + (swap! max-area max area))) + (swap! stack conj [k v]) + (max ans @max-area)) + 0 decorated))) + (defn -main "Run Task 2 with a list of integers, defaulting to the sample given in the task description." [& args] (let [A (if (not-empty args) (map edn/read-string args) [2, 1, 4, 5, 3, 7])] - (println A))) - -(-main) + (prn (largest-rectangle-histogram A)))) diff --git a/challenge-075/tyler-wardhaugh/clojure/test/tw/weekly/c75_test.clj b/challenge-075/tyler-wardhaugh/clojure/test/tw/weekly/c75_test.clj index 856991c00c..c9121cd68a 100644 --- a/challenge-075/tyler-wardhaugh/clojure/test/tw/weekly/c75_test.clj +++ b/challenge-075/tyler-wardhaugh/clojure/test/tw/weekly/c75_test.clj @@ -2,7 +2,7 @@ (:require [clojure.test :refer :all] [multiset.core :as ms] [tw.weekly.ch-1 :refer [find-coin-sum]] - [tw.weekly.ch-2 :refer []])) + [tw.weekly.ch-2 :refer [largest-rectangle-histogram]])) (deftest ch-1 (testing "Task 1" @@ -11,5 +11,5 @@ (deftest ch-2 (testing "Task 2" - - )) + (is (= 12 (largest-rectangle-histogram [2 1 4 5 3 7]))) + (is (= 15 (largest-rectangle-histogram [3 2 3 5 7 5]))))) |
