newb_ie

HackerRank The Coin Change Problem

Jul 20th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.47 KB | None | 0 0
  1. long grid[50][260];
  2. long solve(int idx,int n,vector<long> & coins){
  3.     if((int) coins.size() == idx or n == 0){
  4.         return  n == 0;
  5.     }
  6.     if(grid[idx][n] != -1){
  7.         return grid[idx][n];
  8.     }
  9.     long res = 0;
  10.     for(int i = 0;i <= n / coins[idx]; i++){
  11.         res += solve(idx + 1,n - (coins[idx] * i),coins);
  12.     }
  13.     return grid[idx][n] = res;
  14. }
  15. long getWays(int n, vector<long> &c) {
  16.     memset(grid,-1,sizeof(grid));
  17.     return solve(0,n,c);
  18. }
Add Comment
Please, Sign In to add comment