(贪心) 944. Delete Columns to Make Sorted
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了(贪心) 944. Delete Columns to Make Sorted,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1604字,纯文字阅读大概需要3分钟。
内容图文
![(贪心) 944. Delete Columns to Make Sorted](/upload/InfoBanner/zyjiaocheng/1306/5379569d30254c8c9004309be8ee52e2.jpg)
We are given an array A
of N
lowercase letter strings, all of the same length.
Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.
For example, if we have an array A = ["
abcdef
","uvwxyz"]
and deletion indices {0, 2, 3}
, then the final array after deletions is ["bef", "vyz"]
, and the remaining columns of A
are ["b"
,"
v"]
, ["e","y"]
, and ["f","z"]
. (Formally, the c
-th column is [A[0][c], A[1][c], ..., A[A.length-1][c]]
.)
Suppose we chose a set of deletion indices D
such that after deletions, each remaining column in A is in non-decreasing sorted order.
Return the minimum possible value of D.length
.
Example 1:
Input: ["cba","daf","ghi"]
Output: 1
Explanation:
After choosing D = {1}, each column ["c","d","g"] and ["a","f","i"] are in non-decreasing sorted order.
If we chose D = {}, then a column ["b","a","h"] would not be in non-decreasing sorted order.
Example 2:
Input: ["a","b"]
Output: 0
Explanation: D = {}
Example 3:
Input: ["zyx","wvu","tsr"]
Output: 3
Explanation: D = {0, 1, 2}
Note:
-
1 <= A.length <= 100
-
1 <= A[i].length <= 1000
------------------------------------------------------------------------------------------------------------------
贪心题,可以用O(n^2)过了
C++代码:
class Solution { public : int minDeletionSize(vector<string>& A) { int ans = 0; for(int j = 0; j < A[0].size(); j++){ //i 和 j 不能颠倒。 for(int i = 0; i < A.size() - 1; i++){ if(A[i][j] > A[i+1][j]){ ans++; break; } } } return ans; } };
原文:https://www.cnblogs.com/Weixu-Liu/p/10822155.html
内容总结
以上是互联网集市为您收集整理的(贪心) 944. Delete Columns to Make Sorted全部内容,希望文章能够帮你解决(贪心) 944. Delete Columns to Make Sorted所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。