mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-20 07:21:02 +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:
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* File: coin_change_greedy.dart
|
||||
* Created Time: 2023-08-11
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
/* コイン交換:貪欲法 */
|
||||
int coinChangeGreedy(List<int> coins, int amt) {
|
||||
// coins リストはソート済みと仮定する
|
||||
int i = coins.length - 1;
|
||||
int count = 0;
|
||||
// 残額がなくなるまで貪欲選択を繰り返す
|
||||
while (amt > 0) {
|
||||
// 残額以下で最も近い硬貨を見つける
|
||||
while (i > 0 && coins[i] > amt) {
|
||||
i--;
|
||||
}
|
||||
// coins[i] を選択する
|
||||
amt -= coins[i];
|
||||
count++;
|
||||
}
|
||||
// 実行可能な解が見つからなければ -1 を返す
|
||||
return amt == 0 ? count : -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
// 貪欲法:大域最適解を保証できる
|
||||
List<int> coins = [1, 5, 10, 20, 50, 100];
|
||||
int amt = 186;
|
||||
int res = coinChangeGreedy(coins, amt);
|
||||
print("\ncoins = $coins, amt = $amt");
|
||||
print("$amt を作るのに必要な最小硬貨枚数は $res");
|
||||
|
||||
// 貪欲法:大域最適解を保証できない
|
||||
coins = [1, 20, 50];
|
||||
amt = 60;
|
||||
res = coinChangeGreedy(coins, amt);
|
||||
print("\ncoins = $coins, amt = $amt");
|
||||
print("$amt を作るのに必要な最小硬貨枚数は $res");
|
||||
print("実際に必要な最小枚数は 3 、つまり 20 + 20 + 20");
|
||||
|
||||
// 貪欲法:大域最適解を保証できない
|
||||
coins = [1, 49, 50];
|
||||
amt = 98;
|
||||
res = coinChangeGreedy(coins, amt);
|
||||
print("\ncoins = $coins, amt = $amt");
|
||||
print("$amt を作るのに必要な最小硬貨枚数は $res");
|
||||
print("実際に必要な最小枚数は 2 、つまり 49 + 49");
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* File: fractional_knapsack.dart
|
||||
* Created Time: 2023-08-11
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
/* 品物 */
|
||||
class Item {
|
||||
int w; // 品物の重さ
|
||||
int v; // 品物の価値
|
||||
|
||||
Item(this.w, this.v);
|
||||
}
|
||||
|
||||
/* 分数ナップサック:貪欲法 */
|
||||
double fractionalKnapsack(List<int> wgt, List<int> val, int cap) {
|
||||
// 重さと価値の 2 属性を持つ品物リストを作成
|
||||
List<Item> items = List.generate(wgt.length, (i) => Item(wgt[i], val[i]));
|
||||
// 単位価値 item.v / item.w の高い順にソートする
|
||||
items.sort((a, b) => (b.v / b.w).compareTo(a.v / a.w));
|
||||
// 貪欲選択を繰り返す
|
||||
double res = 0;
|
||||
for (Item item in items) {
|
||||
if (item.w <= cap) {
|
||||
// 残り容量が十分なら、現在の品物を丸ごとナップサックに入れる
|
||||
res += item.v;
|
||||
cap -= item.w;
|
||||
} else {
|
||||
// 残り容量が足りない場合は、現在の品物の一部だけをナップサックに入れる
|
||||
res += item.v / item.w * cap;
|
||||
// 残り容量がないため、ループを抜ける
|
||||
break;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
List<int> wgt = [10, 20, 30, 40, 50];
|
||||
List<int> val = [50, 120, 150, 210, 240];
|
||||
int cap = 50;
|
||||
|
||||
// 貪欲法
|
||||
double res = fractionalKnapsack(wgt, val, cap);
|
||||
print("ナップサック容量を超えない最大価値は $res");
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* File: max_capacity.dart
|
||||
* Created Time: 2023-08-11
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
import 'dart:math';
|
||||
|
||||
/* 最大容量:貪欲法 */
|
||||
int maxCapacity(List<int> ht) {
|
||||
// i, j を初期化し、それぞれ配列の両端に置く
|
||||
int i = 0, j = ht.length - 1;
|
||||
// 初期の最大容量は 0
|
||||
int res = 0;
|
||||
// 2 枚の板が出会うまで貪欲選択を繰り返す
|
||||
while (i < j) {
|
||||
// 最大容量を更新する
|
||||
int cap = min(ht[i], ht[j]) * (j - i);
|
||||
res = max(res, cap);
|
||||
// 短い方を内側へ動かす
|
||||
if (ht[i] < ht[j]) {
|
||||
i++;
|
||||
} else {
|
||||
j--;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
List<int> ht = [3, 8, 5, 2, 7, 7, 3, 4];
|
||||
|
||||
// 貪欲法
|
||||
int res = maxCapacity(ht);
|
||||
print("最大容量は $res");
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* File: max_product_cutting.dart
|
||||
* Created Time: 2023-08-11
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
import 'dart:math';
|
||||
|
||||
/* 最大切断積:貪欲法 */
|
||||
int maxProductCutting(int n) {
|
||||
// n <= 3 のときは、必ず 1 を切り出す
|
||||
if (n <= 3) {
|
||||
return 1 * (n - 1);
|
||||
}
|
||||
// 貪欲に 3 を切り出し、a を 3 の個数、b を余りとする
|
||||
int a = n ~/ 3;
|
||||
int b = n % 3;
|
||||
if (b == 1) {
|
||||
// 余りが 1 のときは、1 * 3 を 2 * 2 に変える
|
||||
return (pow(3, a - 1) * 2 * 2).toInt();
|
||||
}
|
||||
if (b == 2) {
|
||||
// 余りが 2 のときは、そのままにする
|
||||
return (pow(3, a) * 2).toInt();
|
||||
}
|
||||
// 余りが 0 のときは、そのままにする
|
||||
return pow(3, a).toInt();
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
int n = 58;
|
||||
|
||||
// 貪欲法
|
||||
int res = maxProductCutting(n);
|
||||
print("最大分割積は $res");
|
||||
}
|
||||
Reference in New Issue
Block a user