-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec10_62.cpp
31 lines (24 loc) Β· 1 KB
/
lec10_62.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
int coinChange(vector<int>& nums, int target) {
int n = nums.size();
vector<int> prev(target + 1, 1e9), cur(target + 1, 1e9);
// Base case: When target is 0, no coins are needed
prev[0] = cur[0] = 0;
// Fill base case for first coin
for (int T = 0; T <= target; T++) {
if (T % nums[0] == 0) prev[T] = T / nums[0];
}
for (int ind = 1; ind < n; ind++) {
for (int T = 0; T <= target; T++) {
int notTake = prev[T];
int take = 1e9;
if (nums[ind] <= T) take = 1 + cur[T - nums[ind]]; // **Use cur, not prev**
cur[T] = min(take, notTake);
}
prev.swap(cur); // **Efficient way to copy instead of prev = cur**
}
int ans = prev[target];
return (ans >= 1e9) ? -1 : ans;
}
};