mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-08 08:05:49 +08:00
docs: add Japanese translate documents (#1812)
* docs: add Japanese documents (`ja/docs`) * docs: add Japanese documents (`ja/codes`) * docs: add Japanese documents * Remove pythontutor blocks in ja/ * Add an empty at the end of each markdown file. * Add the missing figures (use the English version temporarily). * Add index.md for Japanese version. * Add index.html for Japanese version. * Add missing index.assets * Fix backtracking_algorithm.md for Japanese version. * Add avatar_eltociear.jpg. Fix image links on the Japanese landing page. * Add the Japanese banner. --------- Co-authored-by: krahets <krahets@163.com>
This commit is contained in:
committed by
GitHub
parent
2487a27036
commit
954c45864b
72
ja/codes/java/chapter_dynamic_programming/coin_change.java
Normal file
72
ja/codes/java/chapter_dynamic_programming/coin_change.java
Normal file
@ -0,0 +1,72 @@
|
||||
/**
|
||||
* File: coin_change.java
|
||||
* Created Time: 2023-07-11
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_dynamic_programming;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class coin_change {
|
||||
/* 硬貨両替:動的プログラミング */
|
||||
static int coinChangeDP(int[] coins, int amt) {
|
||||
int n = coins.length;
|
||||
int MAX = amt + 1;
|
||||
// DPテーブルを初期化
|
||||
int[][] dp = new int[n + 1][amt + 1];
|
||||
// 状態遷移:最初の行と最初の列
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
dp[0][a] = MAX;
|
||||
}
|
||||
// 状態遷移:残りの行と列
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
if (coins[i - 1] > a) {
|
||||
// 目標金額を超える場合、硬貨 i を選択しない
|
||||
dp[i][a] = dp[i - 1][a];
|
||||
} else {
|
||||
// 選択しない場合と硬貨 i を選択する場合のより小さい値
|
||||
dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n][amt] != MAX ? dp[n][amt] : -1;
|
||||
}
|
||||
|
||||
/* 硬貨両替:空間最適化動的プログラミング */
|
||||
static int coinChangeDPComp(int[] coins, int amt) {
|
||||
int n = coins.length;
|
||||
int MAX = amt + 1;
|
||||
// DPテーブルを初期化
|
||||
int[] dp = new int[amt + 1];
|
||||
Arrays.fill(dp, MAX);
|
||||
dp[0] = 0;
|
||||
// 状態遷移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
if (coins[i - 1] > a) {
|
||||
// 目標金額を超える場合、硬貨 i を選択しない
|
||||
dp[a] = dp[a];
|
||||
} else {
|
||||
// 選択しない場合と硬貨 i を選択する場合のより小さい値
|
||||
dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[amt] != MAX ? dp[amt] : -1;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] coins = { 1, 2, 5 };
|
||||
int amt = 4;
|
||||
|
||||
// 動的プログラミング
|
||||
int res = coinChangeDP(coins, amt);
|
||||
System.out.println("目標金額を作るのに必要な最小硬貨数は " + res + " です");
|
||||
|
||||
// 空間最適化動的プログラミング
|
||||
res = coinChangeDPComp(coins, amt);
|
||||
System.out.println("目標金額を作るのに必要な最小硬貨数は " + res + " です");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user