LC 955. Delete Columns to Make Sorted II

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.

Runtime: 8 ms, faster than 100.00% of C++ online submissions for Delete Columns to Make Sorted II.

Memory Usage: 819.2 KB, less than 48.15% of C++ online submissions for Delete Columns to Make Sorted II.

class Solution {
public:
  int minDeletionSize(vector<string>& A) {
    int ret = 0;
    bool sorted[A.size()];
    for(int i=0; i<A.size(); i++) sorted[i] = false;
    for(int i=0; i<A[0].size(); i++) {
      int j = 0;
      for(; j<A.size()-1; j++) {
        if(!sorted[j] && (A[j][i] > A[j+1][i])) {
          ret++;
          break;
        }
      }
      if(j < A.size()-1) continue;
      j = 0;
      for(; j<A.size()-1; j++) {
        if(A[j][i] < A[j+1][i]) sorted[j] = true;
      }
    }
    return ret;
  }
};

原文地址:https://www.cnblogs.com/ethanhong/p/10351934.html

时间: 2024-11-06 03:51:59

LC 955. Delete Columns to Make Sorted II的相关文章

[LeetCode 955] Delete Columns to Make Sorted II

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",&qu

[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",&quo

Weekly Contest 111--------&gt;944. 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 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

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

LC.153.Find Minimum in Rotated Sorted Array

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element.

[LeetCode] Max Chunks To Make Sorted II 可排序的最大块之二

This question is the same as "Max Chunks to Make Sorted" except the integers of the given array are not necessarily distinct, the input array could be up to length 2000, and the elements could be up to 10**8. Given an array arr of integers (not

LC 450. Delete Node in a BST

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages: Search for a node to remove. If the n

Max Chunks To Make Sorted II LT768

This question is the same as "Max Chunks to Make Sorted" except the integers of the given array are not necessarily distinct, the input array could be up to length 2000, and the elements could be up to 10**8. Given an array arr of integers (not