[LeetCode/Python] 1464. Maximum Product of Two Elements in an Array
2025. 4. 10. 20:26
https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/description/
접근 방법
(Bubble Sort 연습용 문제)
1. Bubble Sort를 통해 전체 array를 정렬한다
2. 정렬된 array에서 (가장 마지막 원소 - 1)와 (뒤에서 두번째 원소 - 1)의 값을 곱한 값을 리턴한다
class Solution:
def maxProduct(self, nums: List[int]) -> int:
n = len(nums)
# Bubble Sort
for i in range(n - 1):
for j in range(n - i - 1):
if nums[j] > nums[j + 1]:
nums[j], nums[j + 1] = nums[j + 1], nums[j]
answer = (nums[-1] - 1) * (nums[-2] - 1)
return answer
'코딩테스트 > LeetCode' 카테고리의 다른 글
[LeetCode/Python] 706. Design HashMap (0) | 2025.04.10 |
---|---|
[LeetCode/Python] 2283. Check if Number Has Equal Digit Count and Digit Value (0) | 2025.04.09 |
[LeetCode/Python] 70. Climbing Stairs (0) | 2025.04.07 |
[LeetCode/Python] 225. Implement Stack using Queues (0) | 2025.04.04 |
[LeetCode/Python] 232. Implement Queue using Stacks (0) | 2025.04.03 |