[LeetCode] 1464. Maximum Product of Two Elements in an Array

Minji·2024년 1월 5일

Maximum Product of Two Elements in an Array - LeetCode

문제 접근 🤔


  • 리스트 nums 를 최대 힙으로 구현하고 최댓값을 차례로 두개를 꺼낸다.
  • 이 두 값을 각각 1씩 빼서 곱한 값을 반환해주면 된다.


놓쳤던 부분 😅


  • 없음


코드 😁


파이썬 코드(50 ms)

import heapq

class Solution:
    def maxProduct(self, nums: List[int]) -> int:
        maxHeap = []
        for n in nums:
            heapq.heappush(maxHeap, (-n, n))
        i = heapq.heappop(maxHeap)[1]
        j = heapq.heappop(maxHeap)[1]
        return (i - 1) * (j - 1)
profile
기록을 좋아하는 프론트엔드 개발자입니다.

0개의 댓글