[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

 

BELATED ARTICLES

more