960. Delete Columns to Make Sorted III
HardView on LeetCode
Time: O(m²·n)
Space: O(m)
Problem Overview
Delete Columns to Make Sorted III (Hard) asks you to solve a structured algorithmic task. This is a common Array / String pattern in coding interviews. LIS-style DP; dp[j] = min deletions to keep columns 0..j with column j included and all strings non-decreasing.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
LIS-style DP; dp[j] = min deletions to keep columns 0..j with column j included and all strings non-decreasing.
Related patterns: Array, String, Dynamic Programming
960.cs
C#
// Approach: LIS-style DP; dp[j] = min deletions to keep columns 0..j with column j included and all strings non-decreasing.
// Time: O(m²·n) Space: O(m)
public class Solution
{
public int MinDeletionSize(string[] strs)
{
int columnCount = strs[0].Length;
int[] dp = Enumerable.Repeat(1, columnCount).ToArray();
for (int currentCol = 1; currentCol < columnCount; ++currentCol)
{
for (int previousCol = 0; previousCol < currentCol; ++previousCol)
{
bool isNonDecreasing = true;
foreach (var str in strs)
{
if (str[previousCol] > str[currentCol])
{
isNonDecreasing = false;
break;
}
}
if (isNonDecreasing)
dp[currentCol] = Math.Max(dp[currentCol], dp[previousCol] + 1);
}
}
int maxIncreasingLength = dp.Max();
return columnCount - maxIncreasingLength;
}
}Was this solution helpful?
Related Problems
- 4. Median of Two Sorted Arrays(Hard)
- 11. Container With Most Water(Medium)
- 12. Integer to Roman(Medium)
- 13. Roman to Integer(Easy)
- 14. Longest Common Prefix(Easy)
- 15. 3Sum(Medium)