Leetcode Problem 2091. Removing Minimum and Maximum From Array

2091. Removing Minimum and Maximum From Array

Leetcode Solutions

Optimal Deletions from Front, Back, or Both Ends

  1. Initialize variables to store the indices of the minimum and maximum elements.
  2. Iterate through the array to find the indices of the minimum and maximum elements.
  3. Calculate the number of deletions if we remove elements from the front only, which is the maximum of the indices of the minimum and maximum elements plus one.
  4. Calculate the number of deletions if we remove elements from the back only, which is the size of the array minus the minimum of the indices of the minimum and maximum elements.
  5. Calculate the number of deletions if we remove elements from both ends, which is the sum of the index of the minimum element plus one and the size of the array minus the index of the maximum element.
  6. Return the minimum of the three calculated values.
UML Thumbnail

Sequential Deletion from Ends

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...