새소식

Programming/1 Day 1 Commit

[LeetCode] 1046. Last Stone Weight

  • -

1 Day 1 Commit Challenge! 

오늘은 리트코드의 last stone weight 를 풀어보았다. 

아직 파이썬에 익숙하지 않아 다양한 함수를 불러오는 데에 어려움이 많다. 

 

 

Last Stone Weight - 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

 

리트 코드의 Easy 단계의 문제들을 계속 풀어나갈 예정이다. 

해답은 이렇게 ...

class Solution(object):
    def lastStoneWeight(self, stones):
        """
        :type stones: List[int]
        :rtype: int
        """
        max_heap = [-x for x in stones]
        heapq.heapify(max_heap)
        for i in xrange(len(stones)-1):
            x,y = -heapq.heappop(max_heap), -heapq.heappop(max_heap)
            heapq.heappush(max_heap, -abs(x-y))
        return -max_heap[0]

 

*문제는 깃헙의 JSON이 제대로 동작하지 않는다. 

해결방법을 아는 사람은 연락 부탁 .. 

 

GitHub - Park-Minjoo/CODINGINTERVIEW_PRACTICE: 1 Day 1 Problem since 2022.4.7

1 Day 1 Problem since 2022.4.7. Contribute to Park-Minjoo/CODINGINTERVIEW_PRACTICE development by creating an account on GitHub.

github.com

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.