{"id":4337,"date":"2018-11-18T13:45:13","date_gmt":"2018-11-18T21:45:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4337"},"modified":"2018-11-18T13:50:00","modified_gmt":"2018-11-18T21:50:00","slug":"leetcode-944-delete-columns-to-make-sorted","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-944-delete-columns-to-make-sorted\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 944. Delete Columns to Make Sorted"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>We are given an array\u00a0<code>A<\/code>\u00a0of\u00a0<code>N<\/code>\u00a0lowercase letter strings, all of the same length.<\/p>\n<p>Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.<\/p>\n<p>For example, if we have a string\u00a0<code>\"<\/code><code>abcdef<\/code><code>\"<\/code>\u00a0and deletion indices\u00a0<code>{0, 2, 3}<\/code>, then the final string after deletion\u00a0is\u00a0<code>\"<\/code><code>bef<\/code><code>\"<\/code>.<\/p>\n<p>Suppose we chose a set of deletion indices\u00a0<code>D<\/code>\u00a0such that after deletions, each remaining column in A is in\u00a0<strong>non-decreasing<\/strong>\u00a0sorted order.<\/p>\n<p>Formally, the\u00a0<code>c<\/code>-th column is\u00a0<code>[A[0][c], A[1][c], ..., A[A.length-1][c]]<\/code><\/p>\n<p>Return the minimum possible value of\u00a0<code>D.length<\/code>.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">[\"cba\",\"daf\",\"ghi\"]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">1<\/span>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre  class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">[\"a\",\"b\"]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">0<\/span>\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre  class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-3-1\">[\"zyx\",\"wvu\",\"tsr\"]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">3<\/span>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= A.length &lt;= 100<\/code><\/li>\n<li><code>1 &lt;= A[i].length &lt;= 1000<\/code><\/li>\n<\/ol>\n<h1><strong>Solution: Simulation<\/strong><\/h1>\n<p>Check descending case column by column.<\/p>\n<p>Time complexity: O(NL)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua, 64 ms\r\nclass Solution {\r\npublic:\r\n  int minDeletionSize(vector&lt;string&gt;&amp; A) {\r\n    int ans = 0;\r\n    for (int c = 0; c &lt; A[0].size(); ++c)\r\n      for (int r = 1; r &lt; A.size(); ++r) {\r\n        if (A[r][c] &lt; A[r - 1][c]) {\r\n          ++ans;\r\n          break;\r\n        }\r\n      }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem We are given an array\u00a0A\u00a0of\u00a0N\u00a0lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[177,179,4],"class_list":["post-4337","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-medium","tag-simulation","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4337"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4337\/revisions"}],"predecessor-version":[{"id":4342,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4337\/revisions\/4342"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}