2018-03-15 01:46:56 +00:00
|
|
|
(defn sum3
|
2018-08-23 01:41:25 +00:00
|
|
|
"Solve the 3SUM problem in O(n^2) time."
|
|
|
|
[s]
|
|
|
|
(def tab @{})
|
|
|
|
(def solutions @{})
|
|
|
|
(def len (length s))
|
2018-11-29 19:03:45 +00:00
|
|
|
(for k 0 len
|
2019-01-07 00:33:27 +00:00
|
|
|
(put tab (s k) k))
|
2018-11-29 19:03:45 +00:00
|
|
|
(for i 0 len
|
|
|
|
(for j 0 len
|
2019-01-07 00:33:27 +00:00
|
|
|
(def k (get tab (- 0 (s i) (s j))))
|
2018-11-29 19:03:45 +00:00
|
|
|
(when (and k (not= k i) (not= k j) (not= i j))
|
|
|
|
(put solutions {i true j true k true} true))))
|
2018-12-27 18:05:29 +00:00
|
|
|
(map keys (keys solutions)))
|
|
|
|
|
|
|
|
(def arr @[2 4 1 3 8 7 -3 -1 12 -5 -8])
|
|
|
|
(print "3sum of " (string/pretty arr) ":")
|
|
|
|
(print (string/pretty (sum3 arr)))
|