[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 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: 1

Example 2:

Input: ["a","b"]
Output: 0

Example 3:

Input: ["zyx","wvu","tsr"]
Output: 3

Note:

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


给出由 N 个小写字母串组成的数组 A,所有小写字母串的长度都相同。

现在,我们可以选择任何一组删除索引,对于每个字符串,我们将删除这些索引中的所有字符。

举个例子,如果字符串为 "abcdef",且删除索引是 {0, 2, 3},那么删除之后的最终字符串为 "bef"

假设我们选择了一组删除索引 D,在执行删除操作之后,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

提示:

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

超出时间限制

 1 class Solution {
 2     func minDeletionSize(_ A: [String]) -> Int {
 3         var n:Int = A.count
 4         var ret:Int = 0
 5         outer:
 6         for i in 0..<A[0].count
 7         {
 8             for j in 0..<(n - 1)
 9             {
10                 if A[j][i] > A[j+1][i]
11                 {
12                     ret += 1
13                     continue outer
14                 }
15             }
16         }
17         return ret
18     }
19 }
20 extension String {
21     //subscript函数可以检索数组中的值
22     //直接按照索引方式截取指定索引的字符
23     subscript (_ i: Int) -> Character {
24         //读取字符
25         get {return self[index(startIndex, offsetBy: i)]}
26     }
27 }

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

时间: 2024-10-01 16:53:35

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

[Swift Weekly Contest 111]LeetCode941. 有效的山脉数组 | Valid Mountain Array

Given an array A of integers, return true if and only if it is a valid mountain array. Recall that A is a mountain array if and only if: A.length >= 3 There exists some i with 0 < i < A.length - 1 such that: A[0] < A[1] < ... A[i-1] < A[

[Swift Weekly Contest 109]LeetCode934. 最短的桥 | Shortest Bridge

In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.) Now, we may change 0s to 1s so as to connect the two islands together to form 1 island. Return the smallest nu

[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 117]LeetCode967. 具有相同连续差异的数字 | Numbers With Same Consecutive Differences

Return all non-negative integers of length N such that the absolute difference between every two consecutive digits is K. Note that every number in the answer must not have leading zeros except for the number 0 itself. For example, 01 has one leading

[Swift Weekly Contest 120]LeetCode979. 在二叉树中分配硬币 | Distribute Coins in Binary Tree

Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are N coins total. In one move, we may choose two adjacent nodes and move one coin from one node to another.  (The move may be from parent to child, or

[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

[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