mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08:00
Format JS and TS code.
This commit is contained in:
@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
/* 零钱兑换:贪心 */
|
||||
function coin_change_greedy(coins, amt) {
|
||||
function coinChangeGreedy(coins, amt) {
|
||||
// 假设 coins 数组有序
|
||||
let i = coins.length - 1;
|
||||
let count = 0;
|
||||
@ -27,22 +27,22 @@ function coin_change_greedy(coins, amt) {
|
||||
// 贪心:能够保证找到全局最优解
|
||||
let coins = [1, 5, 10, 20, 50, 100];
|
||||
let amt = 186;
|
||||
let res = coin_change_greedy(coins, amt);
|
||||
let res = coinChangeGreedy(coins, amt);
|
||||
console.log(`\ncoins = ${coins}, amt = ${amt}`);
|
||||
console.log(`凑到 ${amt} 所需的最少硬币数量为 ${res}`);
|
||||
|
||||
// 贪心:无法保证找到全局最优解
|
||||
coins = [1, 20, 50];
|
||||
amt = 60;
|
||||
res = coin_change_greedy(coins, amt);
|
||||
res = coinChangeGreedy(coins, amt);
|
||||
console.log(`\ncoins = ${coins}, amt = ${amt}`);
|
||||
console.log(`凑到 ${amt} 所需的最少硬币数量为 ${res}`);
|
||||
console.log("实际上需要的最少数量为 3 ,即 20 + 20 + 20");
|
||||
console.log('实际上需要的最少数量为 3 ,即 20 + 20 + 20');
|
||||
|
||||
// 贪心:无法保证找到全局最优解
|
||||
coins = [1, 49, 50];
|
||||
amt = 98;
|
||||
res = coin_change_greedy(coins, amt);
|
||||
res = coinChangeGreedy(coins, amt);
|
||||
console.log(`\ncoins = ${coins}, amt = ${amt}`);
|
||||
console.log(`凑到 ${amt} 所需的最少硬币数量为 ${res}`);
|
||||
console.log("实际上需要的最少数量为 2 ,即 49 + 49");
|
||||
console.log('实际上需要的最少数量为 2 ,即 49 + 49');
|
||||
|
||||
@ -13,11 +13,11 @@ class Item {
|
||||
}
|
||||
|
||||
/* 分数背包:贪心 */
|
||||
function fractional_knapsack(wgt, val, cap) {
|
||||
function fractionalKnapsack(wgt, val, cap) {
|
||||
// 创建物品列表,包含两个属性:重量、价值
|
||||
const items = wgt.map((w, i) => new Item(w, val[i]));
|
||||
// 按照单位价值 item.v / item.w 从高到低进行排序
|
||||
items.sort((a, b) => (b.v / b.w) - (a.v / a.w));
|
||||
items.sort((a, b) => b.v / b.w - a.v / a.w);
|
||||
// 循环贪心选择
|
||||
let res = 0;
|
||||
for (const item of items) {
|
||||
@ -42,5 +42,5 @@ const cap = 50;
|
||||
const n = wgt.length;
|
||||
|
||||
// 贪心算法
|
||||
const res = fractional_knapsack(wgt, val, cap);
|
||||
const res = fractionalKnapsack(wgt, val, cap);
|
||||
console.log(`不超过背包容量的最大物品价值为 ${res}`);
|
||||
|
||||
@ -5,9 +5,10 @@
|
||||
*/
|
||||
|
||||
/* 最大容量:贪心 */
|
||||
function max_capacity(ht) {
|
||||
function maxCapacity(ht) {
|
||||
// 初始化 i, j 分列数组两端
|
||||
let i = 0, j = ht.length - 1;
|
||||
let i = 0,
|
||||
j = ht.length - 1;
|
||||
// 初始最大容量为 0
|
||||
let res = 0;
|
||||
// 循环贪心选择,直至两板相遇
|
||||
@ -29,5 +30,5 @@ function max_capacity(ht) {
|
||||
const ht = [3, 8, 5, 2, 7, 7, 3, 4];
|
||||
|
||||
// 贪心算法
|
||||
const res = max_capacity(ht);
|
||||
const res = maxCapacity(ht);
|
||||
console.log(`最大容量为 ${res}`);
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
/* 最大切分乘积:贪心 */
|
||||
function max_product_cutting(n) {
|
||||
function maxProductCutting(n) {
|
||||
// 当 n <= 3 时,必须切分出一个 1
|
||||
if (n <= 3) {
|
||||
return 1 * (n - 1);
|
||||
@ -29,5 +29,5 @@ function max_product_cutting(n) {
|
||||
let n = 58;
|
||||
|
||||
// 贪心算法
|
||||
let res = max_product_cutting(n);
|
||||
let res = maxProductCutting(n);
|
||||
console.log(`最大切分乘积为 ${res}`);
|
||||
|
||||
Reference in New Issue
Block a user