SHARE
TWEET

Untitled

a guest Aug 19th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def combination_sum(candidates, target)
  2.   tmp, res = [], []
  3.   sub_sum(candidates.sort!, tmp, target, res)
  4.   res
  5. end
  6.  
  7. def sub_sum(candidates, tmp, target, res)
  8.   return if target < 0
  9.   res << tmp.dup if target == 0
  10.  
  11.   candidates.each_with_index do |c, i|
  12.     sub_sum(candidates[i..-1], tmp << c, target - c, res)
  13.     tmp.pop
  14.   end
  15. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top