코딩테스트/Python

[LeetCode] 1732. Find the Highest Altitude

swwho 2025. 4. 29. 18:06
728x90
반응형

🔗 Problem Link

https://leetcode.com/problems/find-the-highest-altitude/description/


❔Thinking

  • 현재의 높이 0에서 주어지는 높이와의 차이를 순차적으로 계산하고(= net gain altitude) 업데이트한다.
  • 모든 net gain altitude 가운데에 가장 큰 값을 반환한다.

💻Solution

def largestAltitude(self, gain: List[int]) -> int:
    net_gain = [0]
    for h in gain:
        net_gain.append(net_gain[-1] + h)
    return max(net_gain)

🗝️keypoint

  1. 리스트 선언의 필요 없이, 두개의 변수를 통해 max 값을 지속적으로 비교하여 답을 찾을 수도 있다.