Skip to content

Instantly share code, notes, and snippets.

@chase-lambert
Created September 4, 2023 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chase-lambert/6ab242436a5952e203f5ed8ed7299a44 to your computer and use it in GitHub Desktop.
Save chase-lambert/6ab242436a5952e203f5ed8ed7299a44 to your computer and use it in GitHub Desktop.
rendezvous with cassidoo challenge: 23-09-04
(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))))
@chase-lambert
Copy link
Author

I found this excellent solution that introduced me to the min-key function in clojure.core:

(defn min-subs [xs k]
  (->> xs
       (partition k 1)
       (apply min-key (fn [part] (apply + part)))))

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment