Advertisement
DeepRest

Combination Sum III

May 10th, 2022
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. class Solution:
  2.     def combinationSum3(self, k: int, n: int) -> List[List[int]]:  
  3.         combos = []
  4.         currCombo = []
  5.        
  6.         def solve(num, rem, sz):
  7.             if sz == k and rem == 0:
  8.                 combos.append(currCombo[:])
  9.                 return
  10.            
  11.             if num > 9 or rem < num or sz >= k:
  12.                 return  
  13.            
  14.             solve(num+1, rem, sz)
  15.            
  16.             currCombo.append(num)
  17.             solve(num+1, rem-num, sz+1)
  18.             currCombo.pop()
  19.        
  20.         solve(1, n, 0)
  21.         return combos
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement