944. Delete Columns to Make Sorted
EasyView on LeetCode
Time: O(n·m)
Space: O(1)
Advertisement
Intuition
Count columns where any adjacent row pair has decreasing characters.
Algorithm
- 1For each column j: if strs[i][j] > strs[i+1][j] for any i, delete this column.
Common Pitfalls
- •Simple per-column check.
944.cs
C#
// Approach: Count columns where any adjacent row pair is out of lexicographic order.
// Time: O(n·m) Space: O(1)
public class Solution
{
public int MinDeletionSize(string[] strs)
{
int columnCount = strs[0].Length;
int rowCount = strs.Length;
int deletionCount = 0;
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
{
for (int rowIndex = 1; rowIndex < rowCount; rowIndex++)
{
if (strs[rowIndex][columnIndex] < strs[rowIndex - 1][columnIndex])
{
deletionCount++;
break;
}
}
}
return deletionCount;
}
}Advertisement
Was this solution helpful?