Mar. 28th, 2012

izard: (Default)
After a post by [livejournal.com profile] antilamer I tried to compose a clojure code that should definitely look better than written in Java. However my skills are still lacking, what I've got looks uglier and is nearly O(n^2) minus laziness.

(defn find-ones [arr k]
 (let [n (count arr)
       sum (reduce + arr)
       subvecs (fn [l] (map #(subvec arr % (+ l %)) (range (inc (- n l)))))
       has-k? (fn [l] [l (reduce + (map #(if (<= k (reduce + %)) k 0) (subvecs l)))])]
   (if (< sum k) Integer/MAX_VALUE
     (first (first (filter #(> (last %) 0) (map has-k? (range k n))))))))

Profile

izard: (Default)
izard

June 2025

S M T W T F S
1234567
891011121314
15161718192021
22 23242526 2728
2930     

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 4th, 2025 11:24 pm
Powered by Dreamwidth Studios