这个最开始想复杂了但是核心思路也就那样后面也参考了更好的写法。时间复杂度O(n+m):
想复杂的版本:
public int MinDeletionSize1(string[] A)
{
if (A.Length == 0)
return 0;
var tmpSet = new HashSet<int>();
for (int aIndex = 1; aIndex < A.Length; aIndex++)
{
var bchars = A[aIndex - 1].ToCharArray(); //之后
var cchars = A[aIndex].ToCharArray(); //当前
for (int i = 0; i < cchars.Length; i++)
{
if (tmpSet.Contains(i))
{
continue;
}
if (cchars[i] < bchars[i])
{
tmpSet.Add(i);
}
}
}
return tmpSet.Count;
}
更好更简便的版本:
public int MinDeletionSize2(string[] A)
{
var count = 0;
for (int i = 0; i < A[0].Length; i++)
{
for (int j = 1; j < A.Length; j++)
{
if (A[j - 1][i] > A[j][i])
{
count++;
break;
}
}
}
return count;
}