ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

MaxSliceSum coding task - Learn to Code - Codility

Find a maximum sum of a compact subsequence of array elements.

app.codility.com

 


๋ฌธ์ œ ํ’€์ด

Max Profit ๋ฌธ์ œ์™€ ๋™์ผํ•˜๊ฒŒ ๋ธŒ๋ฃจํŠธ ํฌ์Šค๋กœ ํ’€์ง€ ์•Š๊ณ , ์นด๋ฐ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์šฉํ•˜๋ฉด  ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 


์ฝ”๋“œ

def solution(A):
    max_sum = sub_sum = A.pop(0)

    for num in A:
        sub_sum = max(sub_sum + num, num)
        max_sum = max(max_sum, sub_sum)

    return max_sum
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€