250x250
Notice
Recent Posts
Recent Comments
Link
반응형
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- List
- set
- CSS
- 리소스모니터링
- javascript
- math
- dfs
- 스프링부트
- deque
- 힙덤프
- NIO
- scanner
- map
- union_find
- BFS
- html
- 스택
- 큐
- Properties
- Calendar
- string
- sql
- Java
- spring boot
- Union-find
- priority_queue
- JPA
- GC로그수집
- date
- alter
Archives
- Today
- Total
매일 조금씩
Leet code (Medium) : 322. Coin Change - JAVA 본문
728x90
반응형
dp의 가장 정석적인 문제라고도 할 수 있는 동전 문제다.
처음에. 뭔가 큰수의 동전부터 갯수를 잡아나가야하는 줄 방향을 잘못잡았는데
꼭 구성 동전들로 뺐을 때, 0이 되어야 하는 건 아니고,
dp로 금액인 amount를 구성하능한 그러니까 amount 보다 합이 작거나 같은 최소 동전의 수를 구하면 된다.
class Solution {
public int coinChange(int[] coins, int amount) {
int[] ans = new int[amount+1];
Arrays.fill(ans, amount+1);
ans[0] = 0;
for(int i = 1; i <= amount; i++){
for(int j = 0; j < coins.length; j++){
if(i - coins[j] >= 0){
ans[i] = Math.min(ans[i], 1 + ans[i-coins[j]]);
}
}
}
return ans[amount] != amount + 1 ? ans[amount] : -1;
}
}
728x90
반응형
'알고리즘 > DP' 카테고리의 다른 글
Leet code (Medium) : 377. Combination Sum IV - JAVA (0) | 2024.10.13 |
---|---|
Leet code (Medium) : 139. Word Break - JAVA (0) | 2024.10.13 |
이코테 DP : 병사 배치하기 [C++] (0) | 2021.05.13 |
이코테 DP : 금광 [C++] (0) | 2021.04.28 |
이코테 DP : 효율적인 화폐 구성 [C++] (0) | 2021.04.23 |