다이나믹 코인 변경 문제에 대한 마지막 코드 섹션을 알아 내는데 어려움이 있습니다. 아래 코드를 포함 시켰습니다.동적 프로그래밍 - 변경 만들기
나는 마지막으로 else
을 알아낼 수 없습니다. 그 시점에서 욕심 많은 알고리즘을 사용해야합니까, 아니면 이미 테이블에있는 값에서 답을 계산할 수 있습니까? 나는이 문제를 이해하려고 노력하면서 열심히 노력했으며, 나는 꽤 가깝다고 생각한다. 이 방법은 테이블을 생성하고 테이블에 저장된 결과를 사용하여 재귀를 사용하지 않고 더 큰 문제를 해결함으로써 일정한 금액의 변경을 수행하는 데 필요한 최소 금액의 동전을 찾습니다.
public static int minCoins(int[] denom, int targetAmount){
int denomPosition; // Position in denom[] where the first spot
// is the largest coin and includes every coin
// smaller.
int currentAmount; // The Amount of money that needs to be made
// remainingAmount <= initalAmount
int[][] table = new int[denom.length][targetAmount+1];
for(denomPosition = denom.length-1 ; denomPosition >= 0 ; denomPosition--) {
for(currentAmount = 0 ; currentAmount <= targetAmount ; currentAmount++){
if (denomPosition == denom.length-1){
table[denomPosition][currentAmount] =
currentAmount/denom[denomPosition];
}
else if (currentAmount<denom[denomPosition]){
table[denomPosition][currentAmount] =
table[denomPosition+1][currentAmount];
}
else{
table[denomPosition][currentAmount] =
table[denomPosition+1][currentAmount]-
table[denomPosition][denom[denomPosition]]-1;
}
}
}
return table[0][targetAmount];
}
This는 "targetAmount"를 만들기 위해 필요한 동전의 양입니다. 메서드가 작동하지만 문제에 대한 내 이해를 돕지 않는다. 또는 다른 사람이 denomPosition 및 currentAmount에 대한 for 루프를 보는 코드의 핵심 줄에 주석을 달았지만 원래의 프로그램과 모든 유사점을 잃어 버릴 수 있습니다. 당신의 도움을 주셔서 감사합니다. –
내 구현은 [여기] (http://people.csail.mit.edu/bdean/6.046/dp/)에 설명 된 "변경 중"문제를 기반으로하며 링크에서 동영상을 본 후에는 분명해야합니다. –