{"id":5211,"date":"2019-05-27T00:17:18","date_gmt":"2019-05-27T07:17:18","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5211"},"modified":"2019-05-27T00:17:25","modified_gmt":"2019-05-27T07:17:25","slug":"leetcode-1051-height-checker","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1051-height-checker\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1051. Height Checker"},"content":{"rendered":"\n<p>Students are asked to stand in non-decreasing order of heights for an annual photo.<\/p>\n\n\n\n<p>Return the minimum number of students not standing in the right positions.&nbsp; (This is the number of students that must move in order for all students to be standing in non-decreasing order of height.)<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input: <\/strong>[1,1,4,2,1,3]\n<strong>Output: <\/strong>3\n<strong>Explanation: <\/strong>\nStudents with heights 4, 3 and the last 1 are not standing in the right positions.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= heights.length &lt;= 100<\/code><\/li><li><code>1 &lt;= heights[i] &lt;= 100<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sorting<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int heightChecker(vector<int>& heights) {\n    vector<int> h(heights);\n    sort(begin(h), end(h));\n    int ans = 0;\n    for (int i = 0; i < h.size(); ++i)\n      ans += (heights[i] != h[i]);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Students are asked to stand in non-decreasing order of heights for an annual photo. Return the minimum number of students not standing in the right&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,15],"class_list":["post-5211","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-sorting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5211","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=5211"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5211\/revisions"}],"predecessor-version":[{"id":5213,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5211\/revisions\/5213"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}