mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-16 15:18:37 +08:00
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
This commit is contained in:
38
ru/codes/java/chapter_greedy/max_capacity.java
Normal file
38
ru/codes/java/chapter_greedy/max_capacity.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* File: max_capacity.java
|
||||
* Created Time: 2023-07-21
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_greedy;
|
||||
|
||||
public class max_capacity {
|
||||
/* Максимальная вместимость: жадный алгоритм */
|
||||
static int maxCapacity(int[] ht) {
|
||||
// Инициализировать i и j так, чтобы они располагались по двум концам массива
|
||||
int i = 0, j = ht.length - 1;
|
||||
// Начальная максимальная вместимость равна 0
|
||||
int res = 0;
|
||||
// Выполнять жадный выбор в цикле, пока две доски не встретятся
|
||||
while (i < j) {
|
||||
// Обновить максимальную вместимость
|
||||
int cap = Math.min(ht[i], ht[j]) * (j - i);
|
||||
res = Math.max(res, cap);
|
||||
// Сдвигать внутрь более короткую сторону
|
||||
if (ht[i] < ht[j]) {
|
||||
i++;
|
||||
} else {
|
||||
j--;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] ht = { 3, 8, 5, 2, 7, 7, 3, 4 };
|
||||
|
||||
// Жадный алгоритм
|
||||
int res = maxCapacity(ht);
|
||||
System.out.println("Максимальная вместимость = " + res);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user