mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-15 14:48:05 +08:00
* Add Russian docs site baseline * Add Russian localized codebase * Polish Russian code wording * Update ru code translation. * Update code translation and chapter covers. * Fix pythontutor extraction. * Add README and landing page. * placeholder of profiles * Use figures of English version * Remove chapter paperbook
35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
/**
|
|
* File: max_capacity.js
|
|
* Created Time: 2023-09-02
|
|
* Author: Justin (xiefahit@gmail.com)
|
|
*/
|
|
|
|
/* Максимальная вместимость: жадный алгоритм */
|
|
function maxCapacity(ht) {
|
|
// Инициализировать i и j так, чтобы они располагались по двум концам массива
|
|
let i = 0,
|
|
j = ht.length - 1;
|
|
// Начальная максимальная вместимость равна 0
|
|
let res = 0;
|
|
// Выполнять жадный выбор в цикле, пока две доски не встретятся
|
|
while (i < j) {
|
|
// Обновить максимальную вместимость
|
|
const cap = Math.min(ht[i], ht[j]) * (j - i);
|
|
res = Math.max(res, cap);
|
|
// Сдвигать внутрь более короткую сторону
|
|
if (ht[i] < ht[j]) {
|
|
i += 1;
|
|
} else {
|
|
j -= 1;
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
/* Driver Code */
|
|
const ht = [3, 8, 5, 2, 7, 7, 3, 4];
|
|
|
|
// Жадный алгоритм
|
|
const res = maxCapacity(ht);
|
|
console.log(`Максимальная вместимость = ${res}`);
|