Files
hello-algo/ru/codes/rust/chapter_greedy/max_capacity.rs
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

37 lines
1.2 KiB
Rust

/*
* File: coin_change_greedy.rs
* Created Time: 2023-07-22
* Author: night-cruise (2586447362@qq.com)
*/
/* Максимальная вместимость: жадный алгоритм */
fn max_capacity(ht: &[i32]) -> i32 {
// Инициализировать i и j так, чтобы они располагались по двум концам массива
let mut i = 0;
let mut j = ht.len() - 1;
// Начальная максимальная вместимость равна 0
let mut res = 0;
// Выполнять жадный выбор в цикле, пока две доски не встретятся
while i < j {
// Обновить максимальную вместимость
let cap = std::cmp::min(ht[i], ht[j]) * (j - i) as i32;
res = std::cmp::max(res, cap);
// Сдвигать внутрь более короткую сторону
if ht[i] < ht[j] {
i += 1;
} else {
j -= 1;
}
}
res
}
/* Driver Code */
fn main() {
let ht = [3, 8, 5, 2, 7, 7, 3, 4];
// Жадный алгоритм
let res = max_capacity(&ht);
println!("Максимальная вместимость = {}", res);
}