[Swift]LeetCode944. 删除列以使之有序 | Delete Columns to Make Sorted
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號(hào):山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/9977742.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強(qiáng)烈建議點(diǎn)擊原文地址閱讀!支持作者!支持原創(chuàng)!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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 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]]
Return the minimum possible value of?D.length.
?Example 1:
Input: ["cba","daf","ghi"] Output: 1Example 2:
Input: ["a","b"] Output: 0Example 3:
Input: ["zyx","wvu","tsr"] Output: 3Note:
給出由?N?個(gè)小寫(xiě)字母串組成的數(shù)組?A,所有小寫(xiě)字母串的長(zhǎng)度都相同。
現(xiàn)在,我們可以選擇任何一組刪除索引,對(duì)于每個(gè)字符串,我們將刪除這些索引中的所有字符。
舉個(gè)例子,如果字符串為?"abcdef",且刪除索引是?{0, 2, 3},那么刪除之后的最終字符串為?"bef"。
假設(shè)我們選擇了一組刪除索引?D,在執(zhí)行刪除操作之后,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提示:
232ms? 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 }?
轉(zhuǎn)載于:https://www.cnblogs.com/strengthen/p/9977742.html
總結(jié)
以上是生活随笔為你收集整理的[Swift]LeetCode944. 删除列以使之有序 | Delete Columns to Make Sorted的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 二分+并查集【bzoj3007】[SDO
- 下一篇: CodeForces#520 div2