Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 프로그래머스 이중우선순위큐
- 디자인 패턴
- 인프콘 2024
- 코딩테스트 준비
- DesignPattern
- 파이썬
- 항해99
- infcon 2024
- JavaScript
- 커스텀 헤더
- 빈 조회 2개 이상
- 전략패턴 #StrategyPattern #디자인패턴
- 개발자부트캠프추천
- spring batch 5.0
- TiL
- @FeignClient
- Spring multimodule
- Python
- 개발자 취업
- 구글 OAuth login
- 빈 충돌
- jwttoken
- 단기개발자코스
- 프로그래머스
- 1주일회고
- 99클럽
- 취업리부트코스
- 디자인패턴
- jwt
- KPT회고
Archives
- Today
- Total
m1ndy5's coding blog
LeetCode 1464. Maximum Product of Two Elements in an Array with Python 본문
알고리즘 with python/알고리즘 스터디
LeetCode 1464. Maximum Product of Two Elements in an Array with Python
정민됴 2024. 1. 5. 10:40https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/
python heapq는 기본적으로 minheap구조로 되어있다.
따라서 max값을 구하고 싶을 때는 -1을 곱해서 힙에 넣고 최솟값을 뺀 뒤 다시 -1을 곱해주면 된다.
import heapq
class Solution:
def maxProduct(self, nums: List[int]) -> int:
nums = [-1 * num for num in nums]
heapq.heapify(nums)
# -1를 곱했기 때문에 1을 빼려면 1을 더해주면 된다 혹은 리턴할 때 1을 빼고 곱해주거나!
a = heapq.heappop(nums)+1
b = heapq.heappop(nums)+1
return a*b
sort로 풀었어도 크게 문제 없는 문제였다.
'알고리즘 with python > 알고리즘 스터디' 카테고리의 다른 글
LeetCode 215. Kth Largest Element in an Array with Python (0) | 2024.01.05 |
---|---|
LeetCode 1337. The K Weakest Rows in a Matrix with Python (1) | 2024.01.05 |
LeetCode 347. Top K Frequent Elements with Python (0) | 2024.01.04 |
LeetCode 3. Longest Substring Without Repeating Characters with Python (1) | 2024.01.04 |
LeetCode 771. Jewels and Stones with Python (1) | 2024.01.04 |