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
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 0ms, memory 17.2MB, accepted 2025-12-29.
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)