π» Algorithm/Python
[Python] μ΄μ½ν - λ‘λ³Άμ΄ λ‘ λ§λ€κΈ° (μ΄μ§νμ)
μ μ£Ό
2021. 10. 20. 00:05
λ¬Έμ
π 첫 λ²μ§Έ μλ (μ ννμ)
- μ΄μ§νμ ννΈμΈ 건 μμ§λ§ μ μ΄μ§νμμ μ¨μΌ νλμ§ λͺ°λμ
- μ λ¨κΈ°μ λμ΄λ₯Ό κ°μ₯ κΈ΄ λ‘μ κΈΈμ΄λ‘ μ€μ νκ³ 1μ© μ€μ¬λκ°λ©΄μ μλ¦° λ‘μ κΈΈμ΄(rest)μ ν©μ κ³μ°
n, m = map(int, input().split())
height = sorted(list(map(int, input().split())), reverse=True)
for i in range(height[0], 0, -1):
rest = list(map(lambda x: x-i if x>i else 0, height))
if sum(rest)>=m:
print(i)
break
β λ λ²μ§Έ μλ (μ΄μ§νμ)
- λ‘μ κΈΈμ΄μ μ λ¨κΈ°μ λμ΄λ 0λΆν° 10μ΅κΉμ§μ μ μ μ€ νλμ΄λ€.
- μ΄λ κ² ν° νμ λ²μλ₯Ό 보면 λ¨μν μ ννμμ μ¬μ©νμμ λ μκ°μ΄κ³Ό νμ μ λ°μ μ μμΌλ―λ‘ μ΄μ§νμμ λ μ¬λ €μΌ νλ€.
n, m = map(int, input().split())
height = list(map(int, input().split()))
low = 0
high = max(height)
answer = 0
while (low <= high):
mid = (low + high) // 2
rest = list(map(lambda x: x-mid if x>mid else 0, height))
if sum(rest)<m:
high = mid - 1
else:
answer = mid
low = mid + 1
print(answer)