You are viewing a single comment's thread. Return to all comments →
C++ solution :-
long getWays(int n, vector<long> c) { if (n == 0) return 1; long arr[n + 1]; for (int i = 0; i < n + 1; i++) arr[i] = 0; for (long coin: c) for (int i = 1; i < n + 1; i++) { if (coin < i) arr[i] += arr[i - coin]; if (coin == i) arr[i]++; } return arr[n]; }
Simplified implementation :-
long getWays(int n, vector<long> c) { long arr[n + 1]; for (int i = 0; i < n + 1; i++) arr[i] = 0; arr[0] = 1; for (long coin: c) for (int i = 1; i < n + 1; i++) if (coin <= i) arr[i] += arr[i - coin]; return arr[n]; }
Seems like cookies are disabled on this browser, please enable them to open this website
The Coin Change Problem
You are viewing a single comment's thread. Return to all comments →
C++ solution :-
Simplified implementation :-