bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 955. Delete Columns to Make Sorted II

955. Delete Columns to Make Sorted II

Leetcode Solutions

Greedy Approach with Column Deletion

  1. Initialize a set unsorted containing indices of all rows except the last one, since we compare each row with the next one.
  2. Initialize res to 0, which will hold the count of columns to delete.
  3. Iterate through each column j.
  4. For each column, check if there is any row i in unsorted such that strs[i][j] > strs[i+1][j].
  5. If such a row is found, increment res as we need to delete this column.
  6. If no such row is found, update unsorted by removing indices where strs[i][j] < strs[i+1][j] as these rows are sorted with respect to the current column.
  7. Return res as the minimum number of columns to delete.
UML Thumbnail

Brute Force Approach with Column Deletion

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...