4112. Maximize Expression of Three Elements
My accepted Python solution to LeetCode problem 4112, Maximize Expression of Three Elements, running in 4ms.
- Difficulty: Easy
- Python
- Runtime 4ms
- Memory 17.5MB
- 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 4ms, memory 17.5MB, accepted 2025-12-31.
class Solution:
def maximizeExpressionOfThree(self, nums: List[int]) -> int:
# To maximize a + b - c, we want the two largest as a and b, smallest as c
sorted_nums = sorted(nums)
# Two largest are at the end, smallest at beginning
return sorted_nums[-1] + sorted_nums[-2] - sorted_nums[0]