본문 바로가기
코딩 테스트/leetcode

Minimum Number Game

by ornni 2024. 6. 5.
728x90
반응형

첫번째 코드

 

heap을 이용해서 순서대로 정렬한 후

처음 숫자와 두번째 숫자를 뽑은 후

두번쨰 숫자, 첫번째 숫자 순서대로 answer에 넣는 것이다.

 

import heapq

class Solution:
    def numberGame(self, nums: List[int]) -> List[int]:
        heapq.heapify(nums)
        answer = []
        
        while len(nums) > 1:
            alice = heapq.heappop(nums)
            bob = heapq.heappop(nums)
            answer.append(bob)
            answer.append(alice)

        return answer

 

통과!


링크

https://github.com/ornni/leetcode/tree/main/2974-minimum-number-game

 

leetcode/2974-minimum-number-game at main · ornni/leetcode

Collection of LeetCode questions to ace the coding interview! - Created using [LeetHub v3](https://github.com/raphaelheinz/LeetHub-3.0) - ornni/leetcode

github.com

 

반응형

'코딩 테스트 > leetcode' 카테고리의 다른 글

Minimum Number of Moves to Seat Everyone  (0) 2024.06.17
Range Sum of BST  (0) 2024.06.14
Search Insert Position  (0) 2024.06.13
Counting Bits  (0) 2024.06.11
Maximum Product of Two Elements in an Array  (0) 2024.05.27