2018-03-28 17:50:06 +00:00
|
|
|
|
|
|
|
(import "examples/iterators.dst")
|
|
|
|
|
2018-03-15 01:46:56 +00:00
|
|
|
(defn sum3
|
2018-05-05 18:41:47 +00:00
|
|
|
"Solve the 3SUM problem in O(n^2) time."
|
2018-03-15 01:46:56 +00:00
|
|
|
[s]
|
|
|
|
(def tab @{})
|
2018-03-15 21:19:31 +00:00
|
|
|
(def solutions @{})
|
2018-03-15 01:46:56 +00:00
|
|
|
(def len (length s))
|
|
|
|
(for [k 0 len]
|
|
|
|
(put tab (get s k) k))
|
|
|
|
(for [i 0 len]
|
|
|
|
(for [j 0 len]
|
|
|
|
(def k (get tab (- 0 (get s i) (get s j))))
|
|
|
|
(when (and k (not= k i) (not= k j) (not= i j))
|
2018-03-15 21:19:31 +00:00
|
|
|
(put solutions {i true j true k true} true))))
|
|
|
|
(iter2array (map (fn [x] (iter2array (keys x))) (keys solutions))))
|