mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-12 15:36:05 +00: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:
@@ -0,0 +1,33 @@
|
||||
"""
|
||||
File: max_capacity.py
|
||||
Created Time: 2023-07-21
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
|
||||
def max_capacity(ht: list[int]) -> int:
|
||||
"""Максимальная вместимость: жадный алгоритм"""
|
||||
# Инициализировать i и j так, чтобы они располагались по двум концам массива
|
||||
i, j = 0, len(ht) - 1
|
||||
# Начальная максимальная вместимость равна 0
|
||||
res = 0
|
||||
# Выполнять жадный выбор в цикле, пока две доски не встретятся
|
||||
while i < j:
|
||||
# Обновить максимальную вместимость
|
||||
cap = min(ht[i], ht[j]) * (j - i)
|
||||
res = max(res, cap)
|
||||
# Сдвигать внутрь более короткую сторону
|
||||
if ht[i] < ht[j]:
|
||||
i += 1
|
||||
else:
|
||||
j -= 1
|
||||
return res
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
ht = [3, 8, 5, 2, 7, 7, 3, 4]
|
||||
|
||||
# Жадный алгоритм
|
||||
res = max_capacity(ht)
|
||||
print(f"Максимальная вместимость = {res}")
|
||||
Reference in New Issue
Block a user