ν°μ€ν 리 λ·°
π¨π» μ½λ©ν
μ€νΈ/Codility
Lesson 9: Maximum Slice Problem → Max Profit
dirmathfl 2020. 6. 11. 22:19728x90
λ°μν
λ¬Έμ
https://app.codility.com/programmers/lessons/9-maximum_slice_problem/max_profit/
λ¬Έμ νμ΄
리μ€νΈμ κ° λ€μ μ£Όμμ κ°κ²©μ λνλ΄κ³ , μΈλ±μ€λ λ μ§μ΄λ€.
μ£Όμμ μ° νμ νΉμ λ μ§μ ν맀νμμ λ κ°μ₯ ν° μ΄λμ λ°ννλ©΄ λλ€.
μ΄λμ΄ μλ κ²½μ°(λ§μ΄λμ€) μλ 0μ λ°ννλ€.
μ½λ
def solution(A):
if len(A) < 2:
return 0
max_profit = None
min_price = A.pop(0)
for price in A:
cur_profit = price - min_price
if price < min_price:
min_price = price
if max_profit is None:
max_profit = cur_profit
else:
max_profit = max(max_profit, cur_profit)
return max_profit if max_profit > 0 else 0
-
λΈλ£¨νΈ ν¬μ€λ‘ νκ² λλ©΄ O(N^2)μ μκ° λ³΅μ‘λλ₯Ό κ°μ§κ² λλ€.
-
μ΅μκ°κ³Ό νμ¬ κ°μ λΉκ΅νμ¬ μ΅λ μ΄μ΅μ΄ λ°μνλμ§ νμΈνλ©΄ O(N)μ μκ° λ³΅μ‘λλ‘ λ¬Έμ λ₯Ό ν μ μλ€.
728x90
λ°μν
'π¨βπ» μ½λ©ν μ€νΈ > Codility' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Lesson 10: Prime and composite numbers β Count Factors (0) | 2020.06.14 |
---|---|
Lesson 9: Maximum Slice Problem β Max Slice Sum (0) | 2020.06.11 |
Lesson 8: Leader β Equil Leader (0) | 2020.06.10 |
Lesson 8: Leader β Dominator (0) | 2020.06.10 |
Lesson 7: Stacks and Queues β Stone Wall (0) | 2020.06.09 |
λκΈ
κΈ λ³΄κ΄ν¨
μ΅κ·Όμ μ¬λΌμ¨ κΈ
μ΅κ·Όμ λ¬λ¦° λκΈ