-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution960.cs
47 lines (39 loc) · 1.08 KB
/
Solution960.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution960
{
/// <summary>
/// 960. Delete Columns to Make Sorted III - Medium
/// <a href="https://leetcode.com/problems/delete-columns-to-make-sorted-iii">See the problem</a>
/// </summary>
public int MinDeletionSize(string[] strs)
{
var n = strs.Length;
var m = strs[0].Length;
var dp = new int[m];
var result = m - 1;
for (var i = 0; i < m; i++)
{
dp[i] = 1;
for (var j = 0; j < i; j++)
{
var valid = true;
for (var k = 0; k < n; k++)
{
if (strs[k][j] > strs[k][i])
{
valid = false;
break;
}
}
if (valid)
{
dp[i] = Math.Max(dp[i], dp[j] + 1);
}
}
result = Math.Min(result, m - dp[i]);
}
return result;
}
}