LeetCode solutions

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

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 4ms, memory 17.5MB, accepted 2025-12-31.

python
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]

Source