1. 程式人生 > >[Swift Weekly Contest 115]LeetCode960. 刪列造序 ||| | Delete Columns to Make Sorted III

[Swift Weekly Contest 115]LeetCode960. 刪列造序 ||| | Delete Columns to Make Sorted III

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 = ["babca","bbazb"] and deletion indices {0, 1, 4}

, then the final array after deletions is ["bc","az"].

Suppose we chose a set of deletion indices D such that after deletions, the final array has every element (row) in lexicographic order.

For clarity, A[0] is in lexicographic order (ie. A[0][0] <= A[0][1] <= ... <= A[0][A[0].length - 1]

), A[1] is in lexicographic order (ie. A[1][0] <= A[1][1] <= ... <= A[1][A[1].length - 1]), and so on.

Return the minimum possible value of D.length.

Example 1:

Input: ["babca","bbazb"]
Output: 3
Explanation: After deleting columns 0, 1, and 4, the final array is A = ["bc", "az"].
Both these rows are individually in lexicographic order (ie. A[0][0] <= A[0][1] and A[1][0] <= A[1][1]).
Note that A[0] > A[1] - the array A isn't necessarily in lexicographic order.

Example 2:

Input: ["edcba"]
Output: 4
Explanation: If we delete less than 4 columns, the only row won't be lexicographically sorted.

Example 3:

Input: ["ghi","def","abc"]
Output: 0
Explanation: All rows are already lexicographically sorted.

Note:

  1. 1 <= A.length <= 100
  2. 1 <= A[i].length <= 100

給定由 N 個小寫字母字串組成的陣列 A,其中每個字串長度相等。

選取一個刪除索引序列,對於 A 中的每個字串,刪除對應每個索引處的字元。

比如,有 A = ["babca","bbazb"],刪除索引序列 {0, 1, 4},刪除後 A 為["bc","az"]

假設,我們選擇了一組刪除索引 D,那麼在執行刪除操作之後,最終得到的陣列的行中的每個元素都是按字典序排列的。

清楚起見,A[0] 是按字典序排列的(即,A[0][0] <= A[0][1] <= ... <= A[0][A[0].length - 1]),A[1] 是按字典序排列的(即,A[1][0] <= A[1][1] <= ... <= A[1][A[1].length - 1]),依此類推。

請你返回 D.length 的最小可能值。

示例 1:

輸入:["babca","bbazb"]
輸出:3
解釋:
刪除 0、1 和 4 這三列後,最終得到的陣列是 A = ["bc", "az"]。
這兩行是分別按字典序排列的(即,A[0][0] <= A[0][1] 且 A[1][0] <= A[1][1])。
注意,A[0] > A[1] —— 陣列 A 不一定是按字典序排列的。

示例 2:

輸入:["edcba"]
輸出:4
解釋:如果刪除的列少於 4 列,則剩下的行都不會按字典序排列。

示例 3:

輸入:["ghi","def","abc"]
輸出:0
解釋:所有行都已按字典序排列。

提示:

  1. 1 <= A.length <= 100
  2. 1 <= A[i].length <= 100

552ms

 1 class Solution {
 2     func minDeletionSize(_ A: [String]) -> Int {
 3         var A = A
 4         var n:Int = A.count
 5         var m:Int = A[0].count
 6         var f:[Int] = [Int](repeating:0,count:102)
 7         var maxd = -1
 8         for i in 0..<m
 9         {
10             f[i] = 1
11             for j in 0..<i
12             {
13                 if smaller(j, i, &A, n)
14                 {
15                     f[i] = max(f[i], f[j] + 1)
16                 }
17             }
18             maxd = max(maxd, f[i])
19         }
20         return m - maxd
21     }
22     
23     func smaller(_ i:Int,_ j:Int,_ A:inout [String],_ n:Int) -> Bool
24     {
25         for k in 0..<n
26         {
27             if A[k][i] > A[k][j]
28             {
29                 return false
30             }
31         }
32         return true
33     }
34 }
35 
36 extension String {        
37     //subscript函式可以檢索陣列中的值
38     //直接按照索引方式擷取指定索引的字元
39     subscript (_ i: Int) -> Character {
40         //讀取字元
41         get {return self[index(startIndex, offsetBy: i)]}
42     }
43 }