[{"data":1,"prerenderedAt":453},["ShallowReactive",2],{"blog-\u002Fblog\u002Fleetcode_container_with_most_water":3},{"id":4,"title":5,"body":6,"category":441,"cover":442,"csdn":442,"date":443,"description":5,"draft":444,"extension":445,"meta":446,"navigation":214,"path":447,"seo":448,"stem":449,"tags":450,"updated":442,"__hash__":452},"blog\u002Fblog\u002Fleetcode_container_with_most_water.md","leetcode 盛最多水的容器",{"type":7,"value":8,"toc":439},"minimark",[9,17,21,24,27,30,114,120,123,129,240,245,250,345,350,354,431,435],[10,11,12],"p",{},[13,14],"img",{"alt":15,"src":16},"在这里插入图片描述","https:\u002F\u002Fpub-5ec96507162a4f0a8e713a916117c9f4.r2.dev\u002Fblog\u002F676b90317a103be610bb7d09162964ae.png#pic_center",[18,19,20],"h1",{"id":20},"暴力遍历法",[10,22,23],{},"i 指向第 1 块板子，j 指向第 2 块板子，计算面积，j 指向第 3 块板子，计算面积，j 指向第 4 块板子，计算面积.............., 可得出 i 指向第 1 块时，最多盛水量。",[10,25,26],{},"i 指向第 1 + 1块板子，j 指向第 3 块板子，计算面积，j 指向第 4 块板子，计算面积，j 指向第 5 块板子，计算面积.............., 可得出 i 指向第 1 + 1块时，最多盛水量。\n.\n.\n.",[10,28,29],{},"最后可得出 i 指向第 1 ，2，3...n 块板子时，最多盛水量。求最大值即可",[31,32,37],"pre",{"className":33,"code":34,"language":35,"meta":36,"style":36},"language-java shiki shiki-themes github-light","class Solution {\n    public int maxArea(int[] height) {\n        int s_max = 0;\n        for (int i = 0; i \u003C height.length - 1; i ++) {\n            for (int j = i + 1; j \u003C height.length; j ++) {\n                int s = (j - i) * Math.min(height[i], height[j]);\n                s_max = Math.max(s, s_max);\n            }\n        }\n        return s_max;\n    }\n}\n","java","",[38,39,40,48,54,60,66,72,78,84,90,96,102,108],"code",{"__ignoreMap":36},[41,42,45],"span",{"class":43,"line":44},"line",1,[41,46,47],{},"class Solution {\n",[41,49,51],{"class":43,"line":50},2,[41,52,53],{},"    public int maxArea(int[] height) {\n",[41,55,57],{"class":43,"line":56},3,[41,58,59],{},"        int s_max = 0;\n",[41,61,63],{"class":43,"line":62},4,[41,64,65],{},"        for (int i = 0; i \u003C height.length - 1; i ++) {\n",[41,67,69],{"class":43,"line":68},5,[41,70,71],{},"            for (int j = i + 1; j \u003C height.length; j ++) {\n",[41,73,75],{"class":43,"line":74},6,[41,76,77],{},"                int s = (j - i) * Math.min(height[i], height[j]);\n",[41,79,81],{"class":43,"line":80},7,[41,82,83],{},"                s_max = Math.max(s, s_max);\n",[41,85,87],{"class":43,"line":86},8,[41,88,89],{},"            }\n",[41,91,93],{"class":43,"line":92},9,[41,94,95],{},"        }\n",[41,97,99],{"class":43,"line":98},10,[41,100,101],{},"        return s_max;\n",[41,103,105],{"class":43,"line":104},11,[41,106,107],{},"    }\n",[41,109,111],{"class":43,"line":110},12,[41,112,113],{},"}\n",[10,115,116,119],{},[13,117],{"alt":15,"src":118},"https:\u002F\u002Fpub-5ec96507162a4f0a8e713a916117c9f4.r2.dev\u002Fblog\u002F0373099b57abc9014ce9e52a7e48c500.png#pic_center","\n缺点也是显而易见，竟然接近 1s ...",[18,121,122],{"id":122},"双指针",[124,125,126],"blockquote",{},[10,127,128],{},"向中间收紧法\ni 指向第一块板子，j 指向最后一块板子，计算面积，\n当 i 指向的板子高度 \u003C j 指向的板子高度 时，\n     i ++\n否则\n     j --\n直到 i j 相遇",[31,130,132],{"className":33,"code":131,"language":35,"meta":36,"style":36},"public int maxArea(int[] height) {\n    int res = 0;\n    int i = 0;\n    int j = height.length - 1;\n    while (i \u003C j) {\n        int area = (j - i) * Math.min(height[i], height[j]);\n        res = Math.max(res, area);\n        if (height[i] \u003C height[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n\n作者：nettee\n链接：https:\u002F\u002Fleetcode-cn.com\u002Fproblems\u002Fcontainer-with-most-water\u002Fsolution\u002Fon-shuang-zhi-zhen-jie-fa-li-jie-zheng-que-xing-tu\u002F\n来源：力扣（LeetCode）\n著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。\n",[38,133,134,139,144,149,154,159,164,169,174,179,184,189,193,198,204,209,216,222,228,234],{"__ignoreMap":36},[41,135,136],{"class":43,"line":44},[41,137,138],{},"public int maxArea(int[] height) {\n",[41,140,141],{"class":43,"line":50},[41,142,143],{},"    int res = 0;\n",[41,145,146],{"class":43,"line":56},[41,147,148],{},"    int i = 0;\n",[41,150,151],{"class":43,"line":62},[41,152,153],{},"    int j = height.length - 1;\n",[41,155,156],{"class":43,"line":68},[41,157,158],{},"    while (i \u003C j) {\n",[41,160,161],{"class":43,"line":74},[41,162,163],{},"        int area = (j - i) * Math.min(height[i], height[j]);\n",[41,165,166],{"class":43,"line":80},[41,167,168],{},"        res = Math.max(res, area);\n",[41,170,171],{"class":43,"line":86},[41,172,173],{},"        if (height[i] \u003C height[j]) {\n",[41,175,176],{"class":43,"line":92},[41,177,178],{},"            i++;\n",[41,180,181],{"class":43,"line":98},[41,182,183],{},"        } else {\n",[41,185,186],{"class":43,"line":104},[41,187,188],{},"            j--;\n",[41,190,191],{"class":43,"line":110},[41,192,95],{},[41,194,196],{"class":43,"line":195},13,[41,197,107],{},[41,199,201],{"class":43,"line":200},14,[41,202,203],{},"    return res;\n",[41,205,207],{"class":43,"line":206},15,[41,208,113],{},[41,210,212],{"class":43,"line":211},16,[41,213,215],{"emptyLinePlaceholder":214},true,"\n",[41,217,219],{"class":43,"line":218},17,[41,220,221],{},"作者：nettee\n",[41,223,225],{"class":43,"line":224},18,[41,226,227],{},"链接：https:\u002F\u002Fleetcode-cn.com\u002Fproblems\u002Fcontainer-with-most-water\u002Fsolution\u002Fon-shuang-zhi-zhen-jie-fa-li-jie-zheng-que-xing-tu\u002F\n",[41,229,231],{"class":43,"line":230},19,[41,232,233],{},"来源：力扣（LeetCode）\n",[41,235,237],{"class":43,"line":236},20,[41,238,239],{},"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。\n",[10,241,242],{},[13,243],{"alt":15,"src":244},"https:\u002F\u002Fpub-5ec96507162a4f0a8e713a916117c9f4.r2.dev\u002Fblog\u002Fff1e2ebb469e07999501276e2654e01b.png#pic_center",[124,246,247],{},[10,248,249],{},"我觉的上面的方法还可以再优化\ni++, j-- 后在加一层判断 移动后的柱子高度 \u003C= 移动前的柱子高度\n继续 移动",[31,251,253],{"className":33,"code":252,"language":35,"meta":36,"style":36},"public int maxArea(int[] height) {\n    int res = 0;\n    int i = 0;\n    int j = height.length - 1;\n    while (i \u003C j) {\n        res = Math.max(res, (j - i) * Math.min(height[i], height[j]));\n        if(height[i] \u003C height[j]) {\n            int tempi = height[i];\n            do{\n                i++;\n            }while(height[i] \u003C= tempi && i \u003C j);\n        }else{\n             int tempj = height[j];\n            do{\n                j--;\n            }while(height[j] \u003C= tempj && i \u003C j);\n        }\n    }\n    return res;\n}\n",[38,254,255,259,263,267,271,275,280,285,290,295,300,305,310,315,319,324,329,333,337,341],{"__ignoreMap":36},[41,256,257],{"class":43,"line":44},[41,258,138],{},[41,260,261],{"class":43,"line":50},[41,262,143],{},[41,264,265],{"class":43,"line":56},[41,266,148],{},[41,268,269],{"class":43,"line":62},[41,270,153],{},[41,272,273],{"class":43,"line":68},[41,274,158],{},[41,276,277],{"class":43,"line":74},[41,278,279],{},"        res = Math.max(res, (j - i) * Math.min(height[i], height[j]));\n",[41,281,282],{"class":43,"line":80},[41,283,284],{},"        if(height[i] \u003C height[j]) {\n",[41,286,287],{"class":43,"line":86},[41,288,289],{},"            int tempi = height[i];\n",[41,291,292],{"class":43,"line":92},[41,293,294],{},"            do{\n",[41,296,297],{"class":43,"line":98},[41,298,299],{},"                i++;\n",[41,301,302],{"class":43,"line":104},[41,303,304],{},"            }while(height[i] \u003C= tempi && i \u003C j);\n",[41,306,307],{"class":43,"line":110},[41,308,309],{},"        }else{\n",[41,311,312],{"class":43,"line":195},[41,313,314],{},"             int tempj = height[j];\n",[41,316,317],{"class":43,"line":200},[41,318,294],{},[41,320,321],{"class":43,"line":206},[41,322,323],{},"                j--;\n",[41,325,326],{"class":43,"line":211},[41,327,328],{},"            }while(height[j] \u003C= tempj && i \u003C j);\n",[41,330,331],{"class":43,"line":218},[41,332,95],{},[41,334,335],{"class":43,"line":224},[41,336,107],{},[41,338,339],{"class":43,"line":230},[41,340,203],{},[41,342,343],{"class":43,"line":236},[41,344,113],{},[10,346,347],{},[13,348],{"alt":15,"src":349},"https:\u002F\u002Fpub-5ec96507162a4f0a8e713a916117c9f4.r2.dev\u002Fblog\u002Ffc9dc88f58a3e8b390f35223d00131fe.png#pic_center",[18,351,353],{"id":352},"最后放一个-有点意思","最后放一个 (有点意思)",[31,355,357],{"className":33,"code":356,"language":35,"meta":36,"style":36},"class Solution {\n    public int maxArea(int[] height) {\n        int i = 0, j = height.length - 1, res = 0;\n        while(i \u003C j){\n            res = height[i] \u003C height[j] ?\n                Math.max(res, (j - i) * height[i++]):\n                Math.max(res, (j - i) * height[j--]);\n        }\n        return res;\n    }\n}\n\n作者：jyd\n链接：https:\u002F\u002Fleetcode-cn.com\u002Fproblems\u002Fcontainer-with-most-water\u002Fsolution\u002Fcontainer-with-most-water-shuang-zhi-zhen-fa-yi-do\u002F\n来源：力扣（LeetCode）\n著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。\n",[38,358,359,363,367,372,377,382,387,392,396,401,405,409,413,418,423,427],{"__ignoreMap":36},[41,360,361],{"class":43,"line":44},[41,362,47],{},[41,364,365],{"class":43,"line":50},[41,366,53],{},[41,368,369],{"class":43,"line":56},[41,370,371],{},"        int i = 0, j = height.length - 1, res = 0;\n",[41,373,374],{"class":43,"line":62},[41,375,376],{},"        while(i \u003C j){\n",[41,378,379],{"class":43,"line":68},[41,380,381],{},"            res = height[i] \u003C height[j] ?\n",[41,383,384],{"class":43,"line":74},[41,385,386],{},"                Math.max(res, (j - i) * height[i++]):\n",[41,388,389],{"class":43,"line":80},[41,390,391],{},"                Math.max(res, (j - i) * height[j--]);\n",[41,393,394],{"class":43,"line":86},[41,395,95],{},[41,397,398],{"class":43,"line":92},[41,399,400],{},"        return res;\n",[41,402,403],{"class":43,"line":98},[41,404,107],{},[41,406,407],{"class":43,"line":104},[41,408,113],{},[41,410,411],{"class":43,"line":110},[41,412,215],{"emptyLinePlaceholder":214},[41,414,415],{"class":43,"line":195},[41,416,417],{},"作者：jyd\n",[41,419,420],{"class":43,"line":200},[41,421,422],{},"链接：https:\u002F\u002Fleetcode-cn.com\u002Fproblems\u002Fcontainer-with-most-water\u002Fsolution\u002Fcontainer-with-most-water-shuang-zhi-zhen-fa-yi-do\u002F\n",[41,424,425],{"class":43,"line":206},[41,426,233],{},[41,428,429],{"class":43,"line":211},[41,430,239],{},[18,432,434],{"id":433},"end","END",[436,437,438],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":36,"searchDepth":50,"depth":50,"links":440},[],"前端",null,"2021-01-07",false,"md",{},"\u002Fblog\u002Fleetcode_container_with_most_water",{"title":5,"description":5},"blog\u002Fleetcode_container_with_most_water",[451],"leetcode","TW2Ma4Bvjs2IZSldvzy2ohDDtpqClMu8o42T58CCONE",1783159721283]