Files
hello-algo/ru/codes/dart/chapter_greedy/max_capacity.dart
T
Yudong Jin 772183705e Add ru version (#1865)
* 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
2026-03-28 04:24:07 +08:00

38 lines
1.1 KiB
Dart

/**
* 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;
// Выполнять жадный выбор в цикле, пока две доски не встретятся
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");
}