Created
September 4, 2023 22:06
-
-
Save chase-lambert/6ab242436a5952e203f5ed8ed7299a44 to your computer and use it in GitHub Desktop.
rendezvous with cassidoo challenge: 23-09-04
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns min-subs | |
(:require [clojure.test :refer [deftest is]])) | |
(defn min-subs [nums k] | |
(let [subarrays (partition k 1 nums)] | |
(reduce (fn [min-array next-array] | |
(let [[sum-a sum-b] | |
(map (partial apply +) [min-array next-array])] | |
(if (< sum-a sum-b) | |
min-array | |
next-array))) | |
(first subarrays) | |
(rest subarrays)))) | |
(deftest min-subs-test | |
(is (= [4 8 9] (min-subs [1 3 20 4 8 9 11] 3))) | |
(is (= [4 4] (min-subs [4 4 4 4 8] 2)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found this excellent solution that introduced me to the
min-key
function in clojure.core:The solution was found at: https://github.com/mebble/rendezvous-with-cassidoo/blob/main/2023-09-04.clj
min-key docs: https://clojuredocs.org/clojure.core/min-key