Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Iterative logic to remove duplicates
- class Solution:
- def combinationSum2(self, A: List[int], tar: int) -> List[List[int]]:
- A.sort(reverse=True)
- st=[]
- ans=[]
- n=len(A)
- def dfs(i,x):
- if x==tar:
- ans.append(st[:])
- if x>=tar:
- return
- prev=float('inf')
- for j in range(i,n):
- if A[j]!=prev:#Avoiding duplicate step here
- st.append(A[j])
- dfs(j+1,x+A[j])
- st.pop()
- prev=A[j]
- dfs(0,0)
- return ans
Add Comment
Please, Sign In to add comment