• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode960.删列造序III|DeleteColumnstoMakeSortedIII

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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: 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: 4
Explanation: If we delete less than 4 columns, the only row won't be lexicographically sorted.

Example 3:

Input: 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

108ms

 1 class Solution {
 2     func minDeletionSize(_ A: [String]) -> Int {
 3         let n = A.count
 4         if (n == 0) {
 5             return 0
 6         }
 7         let m = A.first!.count
 8         if (m == 0) {
 9             return 0
10         }
11         
12         let Chars = A.map{Array<UInt8>($0.utf8)}
13         
14         var compare = Array<Array<Bool>>(repeating: Array<Bool>(repeating: false, count: m), count: m)
15         for i in 0..<m {
16             one: for j in i+1..<m {
17                 for l in 0..<n {
18                     if Chars[l][i] > Chars[l][j] {
19                         continue one
20                     }
21                 }
22                 compare[i][j] = true
23             }
24         }
25         
26         var f :[Int] = Array<Int>(repeating: 0, count: m)
27         
28         var i = m - 2;
29         while  i >= 0 {
30             var max = 0
31             for j in i+1..<m {
32                 let tempMax = (compare[i][j] ? (1 + f[j]): 0)
33                 if (tempMax > max) {
34                     max = tempMax
35                 }
36             }
37             f[i] = max
38             i -= 1
39         }
40         var max = 0
41         for fe in f {
42             if max < fe {
43                 max = fe
44             }
45         }
46         return m - (max + 1)
47     }
48 }

160ms

 1 class Solution {
 2     func minDeletionSize(_ A: [String]) -> Int {
 3     let count = A[0].count
 4     guard count > 1 else { return 0 }
 5     
 6     let chars: [[Character]] = A.map { Array($0) }
 7     var dp = [Int](repeating: 1, count: count)
 8     
 9     for r in 1..<count {
10         var longest = 0
11         
12         next: for l in 0..<r {
13             for row in chars {
14                 if row[l] > row[r] {
15                     continue next
16                 }
17             }
18             if dp[l] > longest {
19                 longest = dp[l]
20             }
21         }
22         
23         dp[r] = longest + 1
24     }
25     return count - dp.max()!
26 }
27 }

512ms

 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 }

1488ms

 1 class Solution {
 2     func minDeletionSize(_ A: [String]) -> Int {
 3         let count = A[0].count
 4     guard count > 1 else { return 0 }
 5     
 6     var dp = [Int](repeating: 1, count: count)
 7     
 8     for r in 1..<count {
 9         var longest = 0
10         
11         next: for l in 0..<r {
12             for row in A {
13                 let chars = Array(row)
14                 if chars[l] > chars[r]{
15                     continue next
16                 }
17             }
18             if dp[l] > longest {
19                 longest = dp[l]
20             }
21         }
22         
23         dp[r] = longest + 1
24     }
25     
26     return count - dp.max()!
27     }
28 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift游戏实战-跑酷熊猫08产生源源不断的移动平台发布时间:2022-07-13
下一篇:
为什么要用GCD-Swift2.x发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap