{"id":3806,"date":"2018-09-01T23:07:48","date_gmt":"2018-09-02T06:07:48","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3806"},"modified":"2018-09-01T23:09:34","modified_gmt":"2018-09-02T06:09:34","slug":"leetcode-899-orderly-queue","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-899-orderly-queue\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 899. Orderly Queue"},"content":{"rendered":"<div class=\"question-description__3U1T\">\n<h1><strong>Problem<\/strong><\/h1>\n<p>A string\u00a0<code>S<\/code>\u00a0of lowercase letters is given.\u00a0 Then, we may make any number of\u00a0<em>moves<\/em>.<\/p>\n<p>In each move, we\u00a0choose one\u00a0of the first\u00a0<code>K<\/code>\u00a0letters (starting from the left), remove it,\u00a0and place it at the end of the string.<\/p>\n<p>Return the lexicographically smallest string we could have after any number of moves.<\/p>\n<div>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>S = <span id=\"example-input-1-1\">\"cba\"<\/span>, K = <span id=\"example-input-1-2\">1<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">\"acb\"<\/span>\r\n<strong>Explanation: <\/strong>\r\nIn the first move, we move the 1st character (\"c\") to the end, obtaining the string \"bac\".\r\nIn the second move, we move the 1st character (\"b\") to the end, obtaining the final result \"acb\".\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>S = <span id=\"example-input-2-1\">\"baaca\"<\/span>, K = <span id=\"example-input-2-2\">3<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">\"aaabc\"<\/span>\r\n<strong>Explanation: <\/strong>\r\nIn the first move, we move the 1st character (\"b\") to the end, obtaining the string \"aacab\".\r\nIn the second move, we move the 3rd character (\"c\") to the end, obtaining the final result \"aaabc\".\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= K &lt;= S.length\u00a0&lt;= 1000<\/code><\/li>\n<li><code>S<\/code>\u00a0consists of lowercase letters only.<\/li>\n<\/ol>\n<h1><strong>Solution: Rotation or Sort?<\/strong><\/h1>\n<p>if \\(k =1\\), we can only rotate the string.<\/p>\n<p>if \\(k &gt; 1\\), we can bubble sort the string.<\/p>\n<p>Time complexity: O(n^2)<\/p>\n<p>Space complexity: O(n)<\/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\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  string orderlyQueue(string S, int K) {\r\n    if (K &gt; 1) {\r\n      sort(begin(S), end(S));\r\n      return S;\r\n    }\r\n    \r\n    string ans = S;\r\n    for (int i = 1; i &lt; S.size(); ++i)\r\n      ans = min(ans, S.substr(i) +  S.substr(0, i));\r\n    return ans;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\n  public String orderlyQueue(String S, int K) {\r\n    if (K &gt; 1) {\r\n      char[] chars = S.toCharArray();\r\n      Arrays.sort(chars);\r\n      return new String(chars);\r\n    }\r\n    \r\n    String ans = S;\r\n    for (int i = 1; i &lt; S.length(); ++i) {\r\n      String tmp = S.substring(i) + S.substring(0, i);\r\n      if (tmp.compareTo(ans) &lt; 0) ans = tmp;        \r\n    }\r\n    return ans;\r\n  }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3 SC O(n^2)<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true\"># Author: Huahua 48 ms\r\nclass Solution:\r\n  def orderlyQueue(self, S, K):\r\n    if K &gt; 1: return ''.join(sorted(S))\r\n    return min([S[i:] + S[0:i] for i in range(0, len(S))])<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3 SC O(n)<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \"># Author: Huahua 36 ms\r\nclass Solution:\r\n  def orderlyQueue(self, S, K):\r\n    if K &gt; 1: return ''.join(sorted(S))\r\n    ans = S\r\n    for i in range(0, len(S)):\r\n      ans = min(ans, S[i:] + S[0:i])\r\n    return ans<\/pre>\n<\/div><\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem A string\u00a0S\u00a0of lowercase letters is given.\u00a0 Then, we may make any number of\u00a0moves. In each move, we\u00a0choose one\u00a0of the first\u00a0K\u00a0letters (starting from the left),&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[217,367,23,4],"class_list":["post-3806","post","type-post","status-publish","format-standard","hentry","category-string","tag-hard","tag-rotation","tag-sort","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3806","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=3806"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3806\/revisions"}],"predecessor-version":[{"id":3810,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3806\/revisions\/3810"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}