소프트웨어에 대한 모든 것

LeetCode 풀기 - 1732. Find the Highest Altitude 본문

알고리즘/LeetCode

LeetCode 풀기 - 1732. Find the Highest Altitude

앤테바 2021. 11. 9. 05:14
반응형

1732. Find the Highest Altitude

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

 

Find the Highest Altitude - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제)

솔루션1)

class Solution:
    def largestAltitude(self, gain: List[int]) -> int:
        # The biker starts his trip on point 0
        altitudes = [0]
        
        # the net gain in altitude between points i and i+1
        [altitudes.append(altitudes[-1] + g) for g in gain]
        
        # Return the highest altitude of a point.
        return max(altitudes)

솔루션2)

accumulate() 함수를 사용하고 초기 값을 0으로 지정한다.

class Solution:
    def largestAltitude(self, gain: List[int]) -> int:
        return max(accumulate(gain, initial=0))

 

반응형
Comments