1 min read
282 words
Problem Statement
leetcode problem link
Solution [accepted]
class Solution:
def maxProduct(self, nums: List[int]) -> int:
arr = nums[:]
for i in range(len(arr)):
arr[i] -= 1
arr.sort()
return arr[-2] * arr[-1]
Editorial
Sort
class Solution:
def maxProduct(self, nums: List[int]) -> int:
nums.sort()
x = nums[-1]
y = nums[-2]
return (x - 1) * (y - 1)
Approach 3: Track Second Biggest
class Solution:
def maxProduct(self, nums: List[int]) -> int:
biggest = 0
second_biggest = 0
for num in nums:
if num > biggest:
second_biggest = biggest
biggest = num
else:
second_biggest = max(second_biggest, num)
return (biggest - 1) * (second_biggest - 1)
Leave a comment