๐Ÿ—๏ธ Algorithm/๐ŸŸฉ ๋ฐฑ์ค€

๐ŸŸฉ [๋ฐฑ์ค€] [Python] [Gold5] 14719๋ฒˆ_๋น—๋ฌผ

Dbswnstjd 2023. 6. 21. 17:24

๋ฌธ์ œ

https://www.acmicpc.net/status?user_id=wnstjd9701&problem_id=14719&from_mine=1 

 

์ฑ„์  ํ˜„ํ™ฉ

 

www.acmicpc.net

ํ’€์ด

# ๋ฐฑ์ค€ 14719๋ฒˆ ๋ฌธ์ œ - ๋น—๋ฌผ
h, w = map(int, input().split())
world = list(map(int, input().split()))

answer = 0
for i in range(1, w-1):
    left_max = max(world[:i])
    right_max = max(world[i+1:])

    compare = min(left_max, right_max)

    if world[i] < compare:
        answer += compare - world[i]

print(answer)

๊ฐ€์žฅ ์ฒ˜์Œ๊ณผ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์—๋Š” ๋ฌผ์ด ์ฐฐ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์ธ๋ฑ์Šค๋Š” 1 ๋ถ€ํ„ฐ w-1 ๊นŒ์ง€์ด๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ํ˜„์žฌ ๋ธ”๋ก์„ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ์„ ํƒ์ƒ‰ํ•œ๋‹ค.

๊ทธ ์ค‘์— ๊ฐ€์žฅ ํฐ ๊ฐ’๋“ค์„ ๊ฐ€์ ธ์™€์„œ ๋‘˜ ์ค‘์— ์ž‘์€ ๋ถ€๋ถ„์„ ์ €์žฅํ•œ๋‹ค. ์ด ๊ฐ’์ด ํ˜„์žฌ ๋ธ”๋ก๋ณด๋‹ค ๋†’๋‹ค๋ฉด ํ˜„์žฌ ๋ธ”๋ก์—  ๋ฌผ์ด ๊ณ ์ผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.