Delete Columns to Make Sorted - Complete Solution Guide
Delete Columns to Make Sorted is LeetCode problem 944, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an array of n strings strs , all of the same length. The strings can be arranged such that there is one on each line, making a grid. For example, strs = ["abc", "bce", "cae"] can be arranged as follows: abc bce cae You want to delete the columns that are not sorted lexicographically . In the above example ( 0-indexed ), columns 0 ( 'a' , 'b' , 'c' ) and 2 ( 'c' , 'e' , 'e' ) are sorted, while column 1 ( 'b' , 'c' , 'a' ) is not, so you would delete column 1. Return the number of co
Detailed Explanation
The problem asks us to analyze a grid of characters formed by an array of strings, all of the same length. We need to determine the number of columns that are *not* sorted lexicographically (i.e., in ascending order). A column is considered sorted if, reading from top to bottom, the characters in that column are in non-decreasing order. The goal is to count how many columns need to be deleted to make the remaining columns sorted.
Solution Approach
The solution iterates through each column of the input array of strings. For each column, it iterates through the rows, comparing adjacent characters to check if they are in non-decreasing order. If it finds a pair of characters that are not in order, it increments a counter (representing the number of columns to delete) and breaks out of the inner loop (because it's confirmed that the column is not sorted). Finally, it returns the total count of columns to delete.
Step-by-Step Algorithm
- Step 1: Initialize a counter `ans` (or `count`) to 0. This will store the number of columns to delete.
- Step 2: Iterate through the columns of the input array `strs` using a loop (from `j = 0` to `length of strs[0] - 1`).
- Step 3: For each column `j`, iterate through the rows of `strs` using another loop (from `i = 1` to `length of strs - 1`).
- Step 4: Inside the inner loop, compare `strs[i][j]` with `strs[i - 1][j]`. If `strs[i][j] < strs[i - 1][j]`, it means the column is not sorted.
- Step 5: If the column is not sorted, increment the `ans` (or `count`) and break out of the inner loop (because we've already determined that the column needs to be deleted).
- Step 6: After iterating through all the columns, return the final value of `ans` (or `count`).
Key Insights
- Insight 1: We need to iterate through each column of the grid independently to check if it's sorted.
- Insight 2: To check if a column is sorted, we compare adjacent characters within the column. If we find any adjacent characters where the lower one is lexicographically smaller than the upper one, the column is not sorted.
- Insight 3: The problem requires a simple counting process, incrementing the count only if a column is found to be unsorted.
Complexity Analysis
Time Complexity: O(n*m)
Space Complexity: O(1)
Topics
This problem involves: Array, String.
Companies
Asked at: Garmin.