★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-sgfigrtb-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
We are given an array A
of N
lowercase letter strings, all of the same length.git
Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.github
For example, if we have a string "
abcdef
"
and deletion indices {0, 2, 3}
, then the final string after deletion is "
bef
"
.數組
Suppose we chose a set of deletion indices D
such that after deletions, each remaining column in A is in non-decreasing sorted order.微信
Formally, the c
-th column is [A[0][c], A[1][c], ..., A[A.length-1][c]]
app
Return the minimum possible value of D.length
.spa
Example 1:code
Input: ["cba","daf","ghi"]
Output: 1
Example 2:orm
Input: ["a","b"]
Output: 0
Example 3:htm
Input: ["zyx","wvu","tsr"]
Output: 3
Note:
1 <= A.length <= 100
1 <= A[i].length <= 1000
給出由 N
個小寫字母串組成的數組 A
,全部小寫字母串的長度都相同。
如今,咱們能夠選擇任何一組刪除索引,對於每一個字符串,咱們將刪除這些索引中的全部字符。
舉個例子,若是字符串爲 "
abcdef
"
,且刪除索引是 {0, 2, 3}
,那麼刪除以後的最終字符串爲 "
bef
"
。
假設咱們選擇了一組刪除索引 D
,在執行刪除操做以後,A
中剩餘的每一列都是有序的。
形式上,第 c
列爲 [A[0][c], A[1][c], ..., A[A.length-1][c]]
返回 D.length
的最小可能值。
示例 1:
輸入:["cba","daf","ghi"] 輸出:1
示例 2:
輸入:["a","b"] 輸出:0
示例 3:
輸入:["zyx","wvu","tsr"] 輸出:3
提示:
1 <= A.length <= 100
1 <= A[i].length <= 1000
1 class Solution { 2 func minDeletionSize(_ A: [String]) -> Int { 3 var dl = 0 4 var Ab : [[UInt8]] = [] 5 for var s in A { 6 Ab.append(Array<UInt8>(s.utf8)) 7 } 8 for var i in 0..<Ab[0].count { 9 for var j in 0..<A.count-1 { 10 if Ab[j][i] > Ab[j+1][i] { 11 dl += 1 12 break 13 } 14 } 15 } 16 return dl 17 } 18 }
264ms
1 class Solution { 2 func minDeletionSize(_ A: [String]) -> Int { 3 guard A.count > 1 else { return 0 } 4 var minSet = Set<Int>() 5 for i in 0..<A.count-1 { 6 let strArr = Array(A[i]) 7 let strArr2 = Array(A[i+1]) 8 for k in 0..<strArr.count { 9 if strArr[k] > strArr2[k] { 10 minSet.insert(k) 11 } 12 } 13 } 14 return minSet.count 15 } 16 }
272ms
1 class Solution { 2 func minDeletionSize(_ A: [String]) -> Int { 3 var chars: [[Character]] = [] 4 chars = A.map { Array($0) } 5 let numColumns = A.first!.count 6 var count = 0 7 for i in 0..<numColumns { 8 inner: for j in 1..<chars.count { 9 if chars[j][i] < chars[j - 1][i] { 10 count += 1 11 break inner 12 } 13 } 14 } 15 return count 16 } 17 }
280ms
1 class Solution { 2 func minDeletionSize(_ A: [String]) -> Int { 3 var deleteCount = 0 4 5 var arr = [[Character]]() 6 for i in 0..<A.count { 7 arr.append(Array(A[i])) 8 } 9 10 for i in 0..<arr[0].count { 11 for j in 1..<arr.count { 12 if arr[j-1][i] > arr[j][i] { 13 deleteCount += 1 14 break 15 } 16 } 17 } 18 return deleteCount 19 } 20 }
384ms
1 class Solution { 2 func minDeletionSize(_ A: [String]) -> Int { 3 var d_size = [Int]() 4 for index_a in A.indices { 5 if (index_a+1) < A.count { 6 let a_s1 = Array(A[index_a]) 7 let a_s2 = Array(A[index_a+1]) 8 for s_i in 0..<a_s1.count { 9 if String(a_s1[s_i]) > String(a_s2[s_i]) && !d_size.contains(s_i) { 10 d_size.append(s_i) 11 } 12 } 13 } 14 15 } 16 return d_size.count 17 } 18 }