0
from copy import copy,deepcopy
from typing import List

class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        dp = [[]]*(target+1)
        # storing the ways in which i th element can form 
        
        for i in range(1,target+1):
            for ele in candidates :
                if i - ele >=0 :
                    li = deepcopy(dp[i-ele])
                    if i == ele :
                        print(dp,'b',i,ele)
                        # dp[i].append([ele])
                        dp[i] = dp[i] + [[ele]] #here
                        print(dp,'change')
                        continue 
                    else:
                        for listt in li :
                            print(i)
                            # dp[i].append(listt+[ele])
                            dp[i] = dp[i] + [listt+[ele]] #here
        print(dp)
        return dp[-1]


candidates = [2,3,6,7]
target = 3
Solution().combinationSum(candidates,target)

if have left print statements to quickly show the unexpected behavior , to recreate just comment #here statements and uncomment the above statements . Can someone explain the reason behind such behavior?

0 Answers0