{"id":4334,"date":"2018-11-18T09:48:08","date_gmt":"2018-11-18T17:48:08","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4334"},"modified":"2018-11-18T09:48:18","modified_gmt":"2018-11-18T17:48:18","slug":"leetcode-942-di-string-match","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-942-di-string-match\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 942. DI String Match"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a string\u00a0<code>S<\/code>\u00a0that\u00a0<strong>only<\/strong>\u00a0contains &#8220;I&#8221; (increase) or &#8220;D&#8221; (decrease), let\u00a0<code>N = S.length<\/code>.<\/p>\n<p>Return\u00a0<strong>any<\/strong>\u00a0permutation\u00a0<code>A<\/code>\u00a0of\u00a0<code>[0, 1, ..., N]<\/code>\u00a0such that for all\u00a0<code>i = 0,\u00a0..., N-1<\/code>:<\/p>\n<ul>\n<li>If\u00a0<code>S[i] == \"I\"<\/code>, then\u00a0<code>A[i] &lt; A[i+1]<\/code><\/li>\n<li>If\u00a0<code>S[i] == \"D\"<\/code>, then\u00a0<code>A[i] &gt; A[i+1]<\/code><\/li>\n<\/ul>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">\"IDID\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">[0,4,1,3,2]<\/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\">\"III\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">[0,1,2,3]<\/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\">\"DDI\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">[3,2,0,1]<\/span><\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= S.length &lt;= 10000<\/code><\/li>\n<li><code>S<\/code>\u00a0only contains characters\u00a0<code>\"I\"<\/code>\u00a0or\u00a0<code>\"D\"<\/code>.<\/li>\n<\/ol>\n<h1><strong>Solution: Greedy<\/strong><\/h1>\n<p>&#8220;I&#8221; -&gt; use the smallest possible number<\/p>\n<p>&#8220;D&#8221; -&gt; use the largest possible number<\/p>\n<p>Time complexity: O(n)<\/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, 44 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; diStringMatch(string S) {\r\n    const int n = S.length();\r\n    vector&lt;int&gt; ans;\r\n    int lo = 0;\r\n    int hi = n;\r\n    for (char c : S) {\r\n      if (c == 'I')\r\n        ans.push_back(lo++);\r\n      else\r\n        ans.push_back(hi--);\r\n    }\r\n    ans.push_back(lo);\r\n    return ans;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a string\u00a0S\u00a0that\u00a0only\u00a0contains &#8220;I&#8221; (increase) or &#8220;D&#8221; (decrease), let\u00a0N = S.length. Return\u00a0any\u00a0permutation\u00a0A\u00a0of\u00a0[0, 1, &#8230;, N]\u00a0such that for all\u00a0i = 0,\u00a0&#8230;, N-1: If\u00a0S[i] == &#8220;I&#8221;,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[434,222,88],"class_list":["post-4334","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-di","tag-easy","tag-greedy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4334","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=4334"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4334\/revisions"}],"predecessor-version":[{"id":4336,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4334\/revisions\/4336"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}