newb_ie

LeetCode Coin Change 2

Jul 20th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int grid[510][5100];
  4.     int solve(int idx,int amount,vector<int> & coins){
  5.         if(idx == (int) coins.size() or amount == 0){
  6.             return amount == 0;
  7.         }
  8.         if(grid[idx][amount] != -1){
  9.             return grid[idx][amount];
  10.         }
  11.         int res = 0;
  12.         for(int i = 0; i <= amount / coins[idx]; i++){
  13.             res += solve(idx + 1,amount - (coins[idx] * i),coins);
  14.         }
  15.         return grid[idx][amount] = res;
  16.     }
  17.    
  18.     int change(int amount, vector<int>& coins) {
  19.         memset(grid,-1,sizeof(grid));
  20.         return solve(0,amount,coins);
  21.     }
  22. };
Add Comment
Please, Sign In to add comment