Leetcode 1509 - Minimum Difference Between Largest and Smallest Value in Three Moves [3/7/24]

Поділитися
Вставка
  • Опубліковано 5 жов 2024
  • Time Taken: ~15 mins
    Tag: LeetCode Medium
    Concept: Sorting, Greedy
    High-level ideas:
    Base case:
    Since we can at most change 3 moves, if nums.size() less than or equal to 3, return 0. Because we can change all numbers to the same value.
    Else:
    It makes sense to want to change numbers that are smallest or largest as to make an effective change to the "minimum diff between largest and smallest value". Essentially, we can think of it as we are "removing" those values from consideration.
    Therefore, it makes sense to sort the array.
    Afterwards, since we can change at most 3 numbers, consider these cases and the answer is the case that returns minDifference:
    Case 1: remove 1st, 2nd smallest element and 1st largest element
    Case 2: remove 1st, 2nd largest element and 1st smallest element
    Case 3: remove 1,2,3 smallest element
    Case 4: remove 1,2,3 largest element

КОМЕНТАРІ •