LeetCode solutions

1574. Maximum Product of Two Elements in an Array

My accepted Python solution to LeetCode problem 1574, Maximum Product of Two Elements in an Array, running in 0ms.

  • Difficulty: Easy
  • Python
  • Runtime 0ms
  • Memory 17.2MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 0ms, memory 17.2MB, accepted 2025-12-29.

python
class Solution:
    def maxProduct(self, nums: List[int]) -> int:
        # Find two largest elements
        # Time: O(n), Space: O(1)
        max1 = max2 = 0
        for num in nums:
            if num > max1:
                max2 = max1
                max1 = num
            elif num > max2:
                max2 = num
        return (max1 - 1) * (max2 - 1)

Source