Leetcode Problem 2567. Minimum Score by Changing Two Elements

2567. Minimum Score by Changing Two Elements

Leetcode Solutions

Minimizing Score by Modifying at Most Two Elements

Algorithm

  1. Check if the length of the array is 3, return 0 if true.
  2. Sort the array in ascending order.
  3. Calculate the high score for the case where the first two elements are replaced: high1 = nums[-1] - nums[2].
  4. Calculate the high score for the case where the last two elements are replaced: high2 = nums[-3] - nums[0].
  5. Calculate the high score for the case where the first and last elements are replaced: high3 = nums[-2] - nums[1].
  6. Return the minimum of high1, high2, and high3.
UML Thumbnail

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...