728x90
문제 설명)
List에 들어있는 value 중 가장 큰 값과 2번째로 큰값을 각각 -1하고 곱하는 문제
아주 간단한 문제이다.
1) sort 이용
def Maximum(nums: list[int]) -> int:
nums.sort()
return (nums[-1] - 1) * (nums[-2] - 1)
a = [3, 4, 5, 2]
print(Maximum(a))
2) heapq 모듈의 nlargest 이용
import heapq
def Maximum(nums: list[int]) -> int:
a, b = heapq.nlargest(2, nums)
return (a - 1) * (b - 1)
728x90
'알고리즘 > 파이썬' 카테고리의 다른 글
[파이썬 알고리즘] 정렬 기초(1) (2) | 2022.05.26 |
---|---|
[파이썬 알고리즘] 1337. leetcode- The K Weakest Rows in a Matrix (0) | 2022.05.25 |
[파이썬 알고리즘]215.leetcode - 배열의 k번째 큰 요소 (0) | 2022.05.25 |
[파이썬 알고리즘] 힙이란??? (0) | 2022.05.25 |
[파이썬 알고리즘]938.leetcode - 이진 탐색 트리(BST) 합의 범위 (0) | 2022.05.25 |