/
githubmirror
/
hello-algo
Обзор
Документация
Войти
/
githubmirror
/
hello-algo
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
en/codes/javascript/chapter_greedy/max_capacity.js
34 строки
820 B
Yudong Jin
Translate all code to English (#1836)
31 дек 2025, 02:44
Не верифицирован
31 дек 2025, 02:44
2778a6f
Код
Авторство
О чём код?
/** * File: max_capacity.js * Created Time: 2023-09-02 * Author: Justin (xiefahit@gmail.com) */ /* Max capacity: Greedy algorithm */ function maxCapacity(ht) { // Initialize i, j to be at both ends of the array let i = 0, j = ht.length - 1; // Initial max capacity is 0 let res = 0; // Loop for greedy selection until the two boards meet while (i < j) { // Update max capacity const cap = Math.min(ht[i], ht[j]) * (j - i); res = Math.max(res, cap); // Move the shorter board inward if (ht[i] < ht[j]) { i += 1; } else { j -= 1; } } return res; } /* Driver Code */ const ht = [3, 8, 5, 2, 7, 7, 3, 4]; // Greedy algorithm const res = maxCapacity(ht); console.log(`Maximum capacity is ${res}`);