mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-28 16:44:22 +00:00
Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4 * Retranslate Japanese code with GPT-5.4
This commit is contained in:
@@ -7,19 +7,19 @@
|
||||
package chapter_dynamic_programming;
|
||||
|
||||
public class unbounded_knapsack {
|
||||
/* 完全ナップサック:動的プログラミング */
|
||||
/* 完全ナップサック問題:動的計画法 */
|
||||
static int unboundedKnapsackDP(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.length;
|
||||
// DPテーブルを初期化
|
||||
// dp テーブルを初期化
|
||||
int[][] dp = new int[n + 1][cap + 1];
|
||||
// 状態遷移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (wgt[i - 1] > c) {
|
||||
// ナップサックの容量を超える場合、アイテム i を選択しない
|
||||
// ナップサック容量を超えるなら品物 i は選ばない
|
||||
dp[i][c] = dp[i - 1][c];
|
||||
} else {
|
||||
// 選択しない場合とアイテム i を選択する場合のより大きい値
|
||||
// 品物 i を選ばない場合と選ぶ場合の大きい方
|
||||
dp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
@@ -27,19 +27,19 @@ public class unbounded_knapsack {
|
||||
return dp[n][cap];
|
||||
}
|
||||
|
||||
/* 完全ナップサック:空間最適化動的プログラミング */
|
||||
/* 完全ナップサック問題:空間最適化後の動的計画法 */
|
||||
static int unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.length;
|
||||
// DPテーブルを初期化
|
||||
// dp テーブルを初期化
|
||||
int[] dp = new int[cap + 1];
|
||||
// 状態遷移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (wgt[i - 1] > c) {
|
||||
// ナップサックの容量を超える場合、アイテム i を選択しない
|
||||
// ナップサック容量を超えるなら品物 i は選ばない
|
||||
dp[c] = dp[c];
|
||||
} else {
|
||||
// 選択しない場合とアイテム i を選択する場合のより大きい値
|
||||
// 品物 i を選ばない場合と選ぶ場合の大きい方
|
||||
dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
@@ -52,12 +52,12 @@ public class unbounded_knapsack {
|
||||
int[] val = { 5, 11, 15 };
|
||||
int cap = 4;
|
||||
|
||||
// 動的プログラミング
|
||||
// 動的計画法
|
||||
int res = unboundedKnapsackDP(wgt, val, cap);
|
||||
System.out.println("ナップサック容量内での最大値は " + res + " です");
|
||||
System.out.println("ナップサック容量を超えない最大価値は " + res);
|
||||
|
||||
// 空間最適化動的プログラミング
|
||||
// 空間最適化後の動的計画法
|
||||
res = unboundedKnapsackDPComp(wgt, val, cap);
|
||||
System.out.println("ナップサック容量内での最大値は " + res + " です");
|
||||
System.out.println("ナップサック容量を超えない最大価値は " + res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user