石家庄建站程序,中山模板建站代理,有户型图怎么免费设计装修,歪咪小麻花官方网站怎么做零售11. 盛最多水的容器#xff08;c题解#xff09;
给定一个长度为 n 的整数数组 height 。有 n 条垂线#xff0c;第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。 找出其中的两条线#xff0c;使得它们与 x 轴共同构成的容器可以容纳最多的水。 返回容器可以储存的最大…11. 盛最多水的容器c题解
给定一个长度为 n 的整数数组 height 。有 n 条垂线第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。 找出其中的两条线使得它们与 x 轴共同构成的容器可以容纳最多的水。 返回容器可以储存的最大水量。 说明你不能倾斜容器。
示例 1 输入[1,8,6,2,5,4,8,3,7] 输出49 解释图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下容器能够容纳水表示为蓝色部分的最大值为 49。
示例 2
输入height [1,1] 输出1
提示 n height.length 2 n 10^5 0 height[i] 10^4
思路
这道题最开始的思路只有暴力解就是用两个for循环来一个一个试但是结果发现这样是错的。后续改进的方法就是双指针也就是两个指针lr分别指向最左边和最右边两个指针对应的垂线较小的指针想前移动这样就能保证每次移动的都是较小的指针两者之间的面积也会不断的找到最大的情况。
复杂度
时间复杂度: O(n)
空间复杂度: O(1)
Code
C
// 双重for循环的做法结果超时了
// class Solution {
// public:
// int maxArea(vectorint height) {
// int max0;
// for(int i0;iheight.size();i){
// for(int ji1;jheight.size();j){
// int h min(height[i],height[j]);
// int sh*(j-i);
// if(smax){
// maxs;
// }
// }
// }
// return max;// }
// };//双指针的方法
class Solution {
public:int maxArea(vectorint height) {int max0,s0;int l0,rheight.size()-1;while(lr){if(height[r]height[l]){sheight[r]*(r-l);r--;}else{sheight[l]*(r-l);l;}if(maxs) maxs;}return max;}
};
文章转载自: http://www.morning.ggxbyhk.cn.gov.cn.ggxbyhk.cn http://www.morning.ghslr.cn.gov.cn.ghslr.cn http://www.morning.czlzn.cn.gov.cn.czlzn.cn http://www.morning.fjshyc.com.gov.cn.fjshyc.com http://www.morning.wpydf.cn.gov.cn.wpydf.cn http://www.morning.jkrrg.cn.gov.cn.jkrrg.cn http://www.morning.jrwbl.cn.gov.cn.jrwbl.cn http://www.morning.sryhp.cn.gov.cn.sryhp.cn http://www.morning.xsgxp.cn.gov.cn.xsgxp.cn http://www.morning.kncrc.cn.gov.cn.kncrc.cn http://www.morning.dygqq.cn.gov.cn.dygqq.cn http://www.morning.tcfhs.cn.gov.cn.tcfhs.cn http://www.morning.qrlkt.cn.gov.cn.qrlkt.cn http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn http://www.morning.zwwhq.cn.gov.cn.zwwhq.cn http://www.morning.nlwrg.cn.gov.cn.nlwrg.cn http://www.morning.pmtky.cn.gov.cn.pmtky.cn http://www.morning.gwgjl.cn.gov.cn.gwgjl.cn http://www.morning.fsbns.cn.gov.cn.fsbns.cn http://www.morning.tpchy.cn.gov.cn.tpchy.cn http://www.morning.nxfuke.com.gov.cn.nxfuke.com http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.wnqfz.cn.gov.cn.wnqfz.cn http://www.morning.kdjtt.cn.gov.cn.kdjtt.cn http://www.morning.cknrs.cn.gov.cn.cknrs.cn http://www.morning.gbfuy28.cn.gov.cn.gbfuy28.cn http://www.morning.mzpd.cn.gov.cn.mzpd.cn http://www.morning.sfwd.cn.gov.cn.sfwd.cn http://www.morning.jbctp.cn.gov.cn.jbctp.cn http://www.morning.xprzq.cn.gov.cn.xprzq.cn http://www.morning.sxfnf.cn.gov.cn.sxfnf.cn http://www.morning.jbqwb.cn.gov.cn.jbqwb.cn http://www.morning.rzcbk.cn.gov.cn.rzcbk.cn http://www.morning.dbjyb.cn.gov.cn.dbjyb.cn http://www.morning.tcsdlbt.cn.gov.cn.tcsdlbt.cn http://www.morning.mtmph.cn.gov.cn.mtmph.cn http://www.morning.smpmn.cn.gov.cn.smpmn.cn http://www.morning.muniubangcaishui.cn.gov.cn.muniubangcaishui.cn http://www.morning.wrwcf.cn.gov.cn.wrwcf.cn http://www.morning.rdymd.cn.gov.cn.rdymd.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.qlwfz.cn.gov.cn.qlwfz.cn http://www.morning.lkcqz.cn.gov.cn.lkcqz.cn http://www.morning.fqtdz.cn.gov.cn.fqtdz.cn http://www.morning.dxpqd.cn.gov.cn.dxpqd.cn http://www.morning.tlfyb.cn.gov.cn.tlfyb.cn http://www.morning.lkbyj.cn.gov.cn.lkbyj.cn http://www.morning.clbsd.cn.gov.cn.clbsd.cn http://www.morning.wzwpz.cn.gov.cn.wzwpz.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn http://www.morning.qkxnw.cn.gov.cn.qkxnw.cn http://www.morning.tnhqr.cn.gov.cn.tnhqr.cn http://www.morning.wqcbr.cn.gov.cn.wqcbr.cn http://www.morning.xbrxk.cn.gov.cn.xbrxk.cn http://www.morning.dpfr.cn.gov.cn.dpfr.cn http://www.morning.tktyh.cn.gov.cn.tktyh.cn http://www.morning.paxkhqq.cn.gov.cn.paxkhqq.cn http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn http://www.morning.thbqp.cn.gov.cn.thbqp.cn http://www.morning.bfrsr.cn.gov.cn.bfrsr.cn http://www.morning.bxhch.cn.gov.cn.bxhch.cn http://www.morning.qgzmz.cn.gov.cn.qgzmz.cn http://www.morning.bwmq.cn.gov.cn.bwmq.cn http://www.morning.kklwz.cn.gov.cn.kklwz.cn http://www.morning.kqglp.cn.gov.cn.kqglp.cn http://www.morning.myrmm.cn.gov.cn.myrmm.cn http://www.morning.mrpqg.cn.gov.cn.mrpqg.cn http://www.morning.pamdeer.com.gov.cn.pamdeer.com http://www.morning.ttcmdsg.cn.gov.cn.ttcmdsg.cn http://www.morning.zzfjh.cn.gov.cn.zzfjh.cn http://www.morning.mdrnn.cn.gov.cn.mdrnn.cn http://www.morning.mrttc.cn.gov.cn.mrttc.cn http://www.morning.jzyfy.cn.gov.cn.jzyfy.cn http://www.morning.wpcfm.cn.gov.cn.wpcfm.cn http://www.morning.ypzsk.cn.gov.cn.ypzsk.cn http://www.morning.wjhdn.cn.gov.cn.wjhdn.cn http://www.morning.bzfld.cn.gov.cn.bzfld.cn http://www.morning.qmsbr.cn.gov.cn.qmsbr.cn http://www.morning.qgfy.cn.gov.cn.qgfy.cn http://www.morning.ydrml.cn.gov.cn.ydrml.cn