Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Try to make 1D dp possible
- This gave TLE. Since I used 2d dp in python.
- Both are kind of coin change varient either add or dont add type. Whats the difference. WHy method 2 gave TLE why other didnot?
- What diff between method 1 and method 2 varient.
- Method 1:can only give min no of denominations
- Method 2:can give min no of denominations and also tells which are the denominations that adds up to given sum value
- '''
- class Solution:
- def numSquares(self, n: int) -> int:
- q=[]
- for i in range(1,int(n**0.5)+1):
- if i*i<=n:
- q.append(i*i)
- else:
- break
- @cache
- def find(s):
- if s==0:return 0
- if s<0:return 0
- ans=float('inf')
- for sq in q:
- if s-sq>=0:
- ans=min(ans,1+find(s-sq))
- return ans
- return find(n)
- class Solution:
- def numSquares(self, n: int) -> int:
- q=[]
- for i in range(1,n+1):
- if i*i<=n:
- q.append(i*i)
- else:
- break
- #print(q)
- @cache
- def find(x,i):
- if x==0:
- return 0
- if x<0 or i>=len(q) or q[i]>x :
- return float('inf')
- else:
- return min(1+find(x-q[i],i),find(x,i+1))
- return find(n,0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement