[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 }

原文地址:https://www.cnblogs.com/strengthen/p/10126555.html

时间: 2024-10-03 10:41:35

[Swift Weekly Contest 115]LeetCode960. 删列造序 ||| | Delete Columns to Make Sorted III的相关文章

leetcode 944. 删列造序(Delete Columns to Make Sorted)

目录 题目描述: 示例 1: 示例 2: 示例 3: 解法: 题目描述: 给定由 N 个小写字母字符串组成的数组 A,其中每个字符串长度相等. 选取一个删除索引序列,对于 A 中的每个字符串,删除对应每个索引处的字符. 所余下的字符串行从上往下读形成列. 比如,有 A = ["abcdef", "uvwxyz"],删除索引序列 {0, 2, 3},删除后 A 为["bef", "vyz"], A 的列分别为["b&q

944. 删列造序

944. 删列造序 描述: 给定由 N 个小写字母字符串组成的数组 A,其中每个字符串长度相等. 删除 操作的定义是:选出一组要删掉的列,删去 A 中对应列中的所有字符,形式上,第 n 列为 [A[0][n], A[1][n], ..., A[A.length-1][n]]). 比如,有 A = ["abcdef", "uvwxyz"], 要删掉的列为 {0, 2, 3},删除后 A 为["bef", "vyz"], A 的列

[Swift Weekly Contest 112]LeetCode947. 移除最多的同行或同列石头 | Most Stones Removed with Same Row or Column

On a 2D plane, we place stones at some integer coordinate points.  Each coordinate point may have at most one stone. Now, a move consists of removing a stone that shares a column or row with another stone on the grid. What is the largest possible num

[Swift Weekly Contest 108]LeetCode931. 下降路径最小和 | Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t

[Swift Weekly Contest 111]LeetCode944. 删除列以使之有序 | Delete Columns to Make Sorted

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 dele

[Swift Weekly Contest 113]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor

Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.

[Swift Weekly Contest 108]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

[Swift Weekly Contest 108]LeetCode929. 独特的电子邮件地址 | Unique Email Addresses

Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], alice is the local name, and leetcode.com is the domain name. Besides lowercase letters, these emails may contain '.'s or '+'s. If you

[Swift Weekly Contest 108]LeetCode932. 漂亮数组 | Beautiful Array

For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such that: For every i < j, there is no k with i < k < j such that A[k] * 2 = A[i] + A[j]. Given N, return any beautiful array A.  (It is guaranteed th