{"id":10839,"date":"2019-01-01T21:28:51","date_gmt":"2019-01-01T21:28:51","guid":{"rendered":"http:\/\/www.softwareeverydayblog.com\/?p=10839"},"modified":"2019-01-09T02:14:56","modified_gmt":"2019-01-09T02:14:56","slug":"leetcode-container-with-most-water","status":"publish","type":"post","link":"https:\/\/www.softwareeverydayblog.com\/?p=10839","title":{"rendered":"[Leetcode] Container With Most Water"},"content":{"rendered":"<p>[<a href=\"https:\/\/leetcode.com\/problems\/container-with-most-water\/description\/\" rel=\"noopener\" target=\"_blank\">Problem Link<\/a>]<\/p>\n<ul>\n<li>Use a typical 2 pointer approach to solve this problem. <strong>At step 1 assume your max container spans two ends<\/strong>.<\/li>\n<li><strong>What is the area of a container given i and j?<\/strong> It is the width of the container (j-i) x height of the container (which is bounded by the min(heights[i], heights[j]). <\/li>\n<li><strong>Moving which of these pointers might possibly increase the area of our container in the next step?<\/strong> It seems like the minimum of the two heights is holding back so lets move the pointer pointing to min(heights[i], heights[j]).<\/li>\n<\/ul>\n<pre lang=\"java\">\r\nclass Solution {\r\n    public int maxArea(int[] height) {\r\n        int i = 0 ; \r\n        int j = height.length-1;\r\n        int max = Integer.MIN_VALUE;\r\n        \r\n        while ( i < j ) {\r\n            int currwidth = j-i;\r\n            int currheight = Math.min(height[i], height[j]);\r\n            max = Math.max(max, currwidth*currheight);\r\n            \r\n            if ( height[i] < height[j] )\r\n                i++;\r\n            else\r\n                j--;\r\n        }\r\n        \r\n        return max;\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>[Problem Link] Use a typical 2 pointer approach to solve this problem. At step 1 assume your max container spans two ends. What is the area of a container given i and j? It is the width of the container (j-i) x height of the container (which is bounded by the min(heights[i], heights[j]). Moving which [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10839","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/10839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10839"}],"version-history":[{"count":3,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/10839\/revisions"}],"predecessor-version":[{"id":10864,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/10839\/revisions\/10864"}],"wp:attachment":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}