1. 程式人生 > >[Swift Weekly Contest 114]LeetCode955. 刪列造序 || | Delete Columns to Make Sorted II

[Swift Weekly Contest 114]LeetCode955. 刪列造序 || | Delete Columns to Make Sorted II

nal column 字符串長度 oss nbsp cas make dex 序列

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"]

.

Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]).

Return the minimum possible value of D.length.

Example 1:

Input: ["ca","bb","ac"]
Output: 1
Explanation: 
After deleting the first column, A = ["a", "b", "c"].
Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]).
We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1.

Example 2:

Input: ["xc","yb","za"]
Output: 0
Explanation: 
A is already in lexicographic order, so we don‘t need to delete anything.
Note that the rows of A are not necessarily in lexicographic order:
ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...)

Example 3:

Input: ["zyx","wvu","tsr"]
Output: 3
Explanation: 
We have to delete every column.

Note:

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

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

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

比如,有 A = ["abcdef", "uvwxyz"],刪除索引序列 {0, 2, 3},刪除後 A["bef", "vyz"]

假設,我們選擇了一組刪除索引 D,那麽在執行刪除操作之後,最終得到的數組的元素是按 字典序(A[0] <= A[1] <= A[2] ... <= A[A.length - 1])排列的,然後請你返回 D.length 的最小可能值。

示例 1:

輸入:["ca","bb","ac"]
輸出:1
解釋: 
刪除第一列後,A = ["a", "b", "c"]。
現在 A 中元素是按字典排列的 (即,A[0] <= A[1] <= A[2])。
我們至少需要進行 1 次刪除,因為最初 A 不是按字典序排列的,所以答案是 1。

示例 2:

輸入:["xc","yb","za"]
輸出:0
解釋:
A 的列已經是按字典序排列了,所以我們不需要刪除任何東西。
註意 A 的行不需要按字典序排列。
也就是說,A[0][0] <= A[0][1] <= ... 不一定成立。

示例 3:

輸入:["zyx","wvu","tsr"]
輸出:3
解釋:
我們必須刪掉每一列。

提示:

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

76ms
 1 class Solution {
 2     func minDeletionSize(_ A: [String]) -> Int {
 3         var n:Int = A.count
 4         var ss:[String] = [String](repeating:"",count:n)
 5         var ret:Int = 0
 6         outer:
 7         for i in 0..<A[0].count
 8         {
 9             for j in 0..<(n - 1)
10             {
11                 if ss[j] == ss[j+1] && A[j].charAt(i) > A[j+1].charAt(i)
12                 {
13                     ret += 1
14                     continue outer
15                 }
16             }
17             for j in 0..<n
18             {
19                 ss[j] += A[j].charAt(i)
20             }
21         }
22         
23         return ret                  
24     }
25 }
26     
27 extension String {    
28     //獲取指定索引位置的字符,返回為字符串形式
29     func charAt(_ num:Int) -> String
30     {
31         return String(self[index(self.startIndex,offsetBy: num)]) 
32     }
33 }

[Swift Weekly Contest 114]LeetCode955. 刪列造序 || | Delete Columns to Make Sorted II