{"id":1855,"date":"2016-03-03T20:43:08","date_gmt":"2016-03-03T12:43:08","guid":{"rendered":"http:\/\/boweihe.me\/?p=1855"},"modified":"2016-03-03T20:43:08","modified_gmt":"2016-03-03T12:43:08","slug":"leetcode-11-container-with-most-water","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1855","title":{"rendered":"LeetCode 11. Container With Most Water"},"content":{"rendered":"<p><strong>\u539f\u9898\uff1a<\/strong>https:\/\/leetcode.com\/problems\/container-with-most-water\/<br \/>\n<strong>\u601d\u8def\uff1a<\/strong><br \/>\n\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5f53\u7136\u662f\u5d4c\u5957\u4e24\u5c42\u7684\u5faa\u73afi,j\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(n^2)\uff0c\u4f46\u662f\u64b8\u4e86\u4e00\u53d1\u8d85\u65f6\u4e86\uff0c\u8bf4\u660e\u8fd8\u6709\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u3002<br \/>\n\u53ef\u4ee5\u8fd9\u4e48\u7406\u89e3\uff0c\u8981\u8ba9\u957f\u65b9\u5f62\u7684\u9762\u79ef\u6700\u5927\uff0c\u53ef\u4ee5\u5148\u4ece\u5e95\u8fb9\u957f\u5ea6\u6700\u5927\u5f00\u59cb\u8bd5\uff0c\u5047\u8bbe\u5de6\u7aef\u9ad8\u5ea6\u662fh_i\uff0c\u53f3\u7aef\u9ad8\u5ea6\u662fh_j\uff0c\u90a3\u4e48\u9762\u79ef\u5c31\u662f(j-i)*min(h_i, h_j).<br \/>\n\u6b64\u65f6\u53ef\u4ee5\u79fb\u52a8\u5de6\u8fb9\u6216\u53f3\u8fb9\u7684\u67f1\u5b50\u5c06\u5e95\u8fb9\u957f\u5ea6\u7f29\u5c0f\u4e00\u70b9\uff0c\u4f46\u662f\u6709\u4e2a\u95ee\u9898\uff0c\u5047\u8bbe\u5f53\u524d\u7684\u74f6\u9888\u5728\u5de6\u4fa7i\u8fd9\u91cc\uff0c\u5982\u679c\u4fdd\u6301i\u7684\u4f4d\u7f6e\u4e0d\u53d8\u79fb\u52a8\u53f3\u4fa7\uff0c\u6700\u597d\u60c5\u51b5\u4e5f\u53ea\u4f1a\u628a\u9762\u79ef\u7f29\u5c0f\uff0c\u56e0\u4e3a\u5e95\u8fb9\u957f\u5ea6\u7f29\u6c34\u4e86\uff0c\u9ad8\u6700\u9ad8\u4e5f\u5c31\u662fi\u7684\u9ad8\u5ea6\uff0c\u6240\u4ee5\u5176\u5b9e\u82e5\u8981\u7f29\u5c0f\u5bbd\u5ea6\uff0c\u53ea\u80fd\u628a\u74f6\u9888\u53bb\u9664\u6389\uff0c\u53ea\u80fd\u79fb\u52a8\u5f53\u524d\u7684\u74f6\u9888\u4f4d\u7f6ei\uff0c\u8fd9\u6837\u5982\u679c\u78b0\u5230\u4e2a\u66f4\u5927\u7684i&#8217;\uff0c\u9ad8\u5ea6\u4f1a\u4e0a\u5347\u81f3min(h_i&#8217;,h_j)\uff0c\u80af\u5b9a\u6bd4i\u8981\u5927\u4e86\uff0c\u8fd9\u6837\u624d\u6709\u53ef\u80fd\u589e\u52a0\u9762\u79ef\u3002\u7531\u4e8e\u8fd9\u79cd\u65b9\u6cd5\u53ea\u7528\u626b\u4e00\u904d\u6570\u7ec4\uff0c\u6240\u4ee5\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(n)<br \/>\n<strong>\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre class=\"lang:c++ decode:true\">class Solution {\npublic:\n    int maxArea(vector&lt;int&gt;&amp; height) {\n        int i = 0;\n        int j = height.size() - 1;\n        int area;\n        int maxArea = 0;\n        while(i != j){\n            if(height[i] &lt; height[j]){\n                area = height[i] * (j - i);\n                i ++; \/\/Bottle neck at i, move forward\n            } else {\n                area = height[j] * (j - i);\n                j --; \/\/Bottle neck at j, move backward\n            }\n            if (area &gt; maxArea)\n                maxArea = area;\n        }\n        return maxArea;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u9898\uff1ahttps:\/\/leetcode.com\/problems\/container-with-most-water\/ \u601d\u8def\uff1a \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5f53\u7136\u662f\u5d4c\u5957\u4e24\u5c42\u7684\u5faa\u73afi,j\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(n^2)\uff0c\u4f46\u662f\u64b8\u4e86\u4e00\u53d1\u8d85\u65f6\u4e86\uff0c\u8bf4\u660e\u8fd8\u6709\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u3002 \u53ef\u4ee5\u8fd9\u4e48\u7406\u89e3\uff0c\u8981\u8ba9\u957f\u65b9\u5f62\u7684\u9762\u79ef\u6700\u5927\uff0c\u53ef\u4ee5\u5148\u4ece\u5e95\u8fb9\u957f\u5ea6\u6700\u5927\u5f00\u59cb\u8bd5\uff0c\u5047\u8bbe\u5de6\u7aef\u9ad8\u5ea6\u662fh_i\uff0c\u53f3\u7aef\u9ad8\u5ea6\u662fh_j\uff0c\u90a3\u4e48\u9762\u79ef\u5c31\u662f(j-i)*min(h_i, h_j). \u6b64\u65f6\u53ef\u4ee5\u79fb\u52a8\u5de6\u8fb9\u6216\u53f3\u8fb9\u7684\u67f1\u5b50\u5c06\u5e95\u8fb9\u957f\u5ea6\u7f29\u5c0f\u4e00\u70b9\uff0c\u4f46\u662f\u6709\u4e2a\u95ee\u9898\uff0c\u5047\u8bbe\u5f53\u524d\u7684\u74f6\u9888\u5728\u5de6\u4fa7i\u8fd9\u91cc\uff0c\u5982\u679c\u4fdd\u6301i\u7684\u4f4d\u7f6e\u4e0d\u53d8\u79fb\u52a8\u53f3\u4fa7\uff0c\u6700\u597d\u60c5\u51b5\u4e5f\u53ea\u4f1a\u628a\u9762\u79ef\u7f29\u5c0f\uff0c\u56e0\u4e3a\u5e95\u8fb9\u957f\u5ea6\u7f29\u6c34\u4e86\uff0c\u9ad8\u6700\u9ad8\u4e5f\u5c31\u662fi\u7684\u9ad8\u5ea6\uff0c\u6240\u4ee5\u5176\u5b9e\u82e5\u8981\u7f29\u5c0f\u5bbd\u5ea6\uff0c\u53ea\u80fd\u628a\u74f6\u9888\u53bb\u9664\u6389\uff0c\u53ea\u80fd\u79fb\u52a8\u5f53\u524d\u7684\u74f6\u9888\u4f4d\u7f6ei\uff0c\u8fd9\u6837\u5982\u679c\u78b0\u5230\u4e2a\u66f4\u5927\u7684i&#8217;\uff0c\u9ad8\u5ea6\u4f1a\u4e0a\u5347\u81f3min(h_i&#8217;,h_j)\uff0c\u80af\u5b9a\u6bd4i\u8981\u5927\u4e86\uff0c\u8fd9\u6837\u624d\u6709\u53ef\u80fd\u589e\u52a0\u9762\u79ef\u3002\u7531\u4e8e\u8fd9\u79cd\u65b9\u6cd5\u53ea\u7528\u626b\u4e00\u904d\u6570\u7ec4\uff0c\u6240\u4ee5\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(n) \u4ee3\u7801\uff1a class Solution { public: int maxArea(vector&lt;int&gt;&amp; height) { int i = 0; int j = height.size() &#8211; 1; int area; int maxArea = 0; while(i != j){ if(height[i] &lt; height[j]){ area = height[i] * (j &#8211; i); i ++; \/\/Bottle neck at i, move forward } else { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66],"class_list":["post-1855","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1855"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1855\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}