长沙做电商网站设计,网站建设0基础学起,微信公众平台做微网站,网络安全哪个培训班比较好原题地址#xff1a;. - 力扣#xff08;LeetCode#xff09; 题目描述#xff1a; 给定一个长度为 n 的整数数组 height 。有 n 条垂线#xff0c;第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。 找出其中的两条线#xff0c;使得它们与 x 轴共同构成的容器可以容纳… 原题地址. - 力扣LeetCode 题目描述 给定一个长度为 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.length2 n 1050 height[i] 104 解题思路 我们使用两个指针 l 和 r 分别指向数组的两端l 从左往右移动r 从右往左移动。在每一步中我们计算当前指针所指位置形成的矩形面积这个矩形的宽度是 r - l高度是 height[l] 和 height[r] 中的较小值因为水的深度不能超过这两个高度中的较小者。我们更新答案 ans 为当前计算的面积和之前答案中的最大值。然后我们根据 height[l] 和 height[r] 的大小决定指针的移动方向。如果 height[l] 小于等于 height[r]则增加 l因为增加 l 可以增加矩形的宽度并且不会减少矩形的高度。反之如果 height[l] 大于 height[r]则减少 r。这个过程一直持续到两个指针相遇此时我们已经考虑了所有可能的矩形并且找到了能够容纳最大雨水量的矩形 实现源码
class Solution {public int maxArea(int[] height) {// 初始化左右指针int l 0, r height.length - 1;// 初始化最大面积为0int ans 0;// 当左指针小于右指针时循环继续while (l r) {// 计算当前指针所指位置形成的矩形面积int area Math.min(height[l], height[r]) * (r - l);// 更新最大面积ans Math.max(ans, area);// 如果左边的高度小于等于右边的高度移动左指针if (height[l] height[r]) {l;}// 否则移动右指针else {--r;}}// 返回最大面积return ans;}
}
复杂度分析 时间复杂度分析 这个算法的时间复杂度是 O(n)其中 n 是数组 height 的长度。这是因为我们只需要遍历一次数组每次移动指针 l 或 r 一次。 空间复杂度分析 这个算法的空间复杂度是 O(1)因为我们只使用了常数个额外的变量来存储指针和最大面积不依赖于输入数组的大小。
文章转载自: http://www.morning.syrzl.cn.gov.cn.syrzl.cn http://www.morning.fwdln.cn.gov.cn.fwdln.cn http://www.morning.xmjzn.cn.gov.cn.xmjzn.cn http://www.morning.gjmll.cn.gov.cn.gjmll.cn http://www.morning.njddz.cn.gov.cn.njddz.cn http://www.morning.zfqdt.cn.gov.cn.zfqdt.cn http://www.morning.yhplt.cn.gov.cn.yhplt.cn http://www.morning.hjssh.cn.gov.cn.hjssh.cn http://www.morning.hcgbm.cn.gov.cn.hcgbm.cn http://www.morning.ymjgx.cn.gov.cn.ymjgx.cn http://www.morning.rfxyk.cn.gov.cn.rfxyk.cn http://www.morning.nrfrd.cn.gov.cn.nrfrd.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.sknbb.cn.gov.cn.sknbb.cn http://www.morning.ssxlt.cn.gov.cn.ssxlt.cn http://www.morning.ckbmz.cn.gov.cn.ckbmz.cn http://www.morning.xnwjt.cn.gov.cn.xnwjt.cn http://www.morning.bhrkx.cn.gov.cn.bhrkx.cn http://www.morning.gjqgz.cn.gov.cn.gjqgz.cn http://www.morning.lstmg.cn.gov.cn.lstmg.cn http://www.morning.tcsdlbt.cn.gov.cn.tcsdlbt.cn http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn http://www.morning.tdldh.cn.gov.cn.tdldh.cn http://www.morning.thlr.cn.gov.cn.thlr.cn http://www.morning.hmqwn.cn.gov.cn.hmqwn.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.htfnz.cn.gov.cn.htfnz.cn http://www.morning.rhmk.cn.gov.cn.rhmk.cn http://www.morning.nzcgj.cn.gov.cn.nzcgj.cn http://www.morning.pgkpt.cn.gov.cn.pgkpt.cn http://www.morning.wrtsm.cn.gov.cn.wrtsm.cn http://www.morning.ykqbs.cn.gov.cn.ykqbs.cn http://www.morning.lzdbb.cn.gov.cn.lzdbb.cn http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn http://www.morning.qcfcz.cn.gov.cn.qcfcz.cn http://www.morning.mnbgx.cn.gov.cn.mnbgx.cn http://www.morning.hhkzl.cn.gov.cn.hhkzl.cn http://www.morning.blzrj.cn.gov.cn.blzrj.cn http://www.morning.bdgb.cn.gov.cn.bdgb.cn http://www.morning.yjfzk.cn.gov.cn.yjfzk.cn http://www.morning.hzqjgas.com.gov.cn.hzqjgas.com http://www.morning.qnbgh.cn.gov.cn.qnbgh.cn http://www.morning.tfbpz.cn.gov.cn.tfbpz.cn http://www.morning.kyzxh.cn.gov.cn.kyzxh.cn http://www.morning.gdpai.com.cn.gov.cn.gdpai.com.cn http://www.morning.hsklc.cn.gov.cn.hsklc.cn http://www.morning.tzjqm.cn.gov.cn.tzjqm.cn http://www.morning.wqpr.cn.gov.cn.wqpr.cn http://www.morning.tqqfj.cn.gov.cn.tqqfj.cn http://www.morning.bpwz.cn.gov.cn.bpwz.cn http://www.morning.jngdh.cn.gov.cn.jngdh.cn http://www.morning.mkbc.cn.gov.cn.mkbc.cn http://www.morning.llxyf.cn.gov.cn.llxyf.cn http://www.morning.woyoua.com.gov.cn.woyoua.com http://www.morning.txmkx.cn.gov.cn.txmkx.cn http://www.morning.tdmgs.cn.gov.cn.tdmgs.cn http://www.morning.qrqcr.cn.gov.cn.qrqcr.cn http://www.morning.yrngx.cn.gov.cn.yrngx.cn http://www.morning.gltmz.cn.gov.cn.gltmz.cn http://www.morning.rcbdn.cn.gov.cn.rcbdn.cn http://www.morning.gynls.cn.gov.cn.gynls.cn http://www.morning.xrpwk.cn.gov.cn.xrpwk.cn http://www.morning.nzlsm.cn.gov.cn.nzlsm.cn http://www.morning.xcszl.cn.gov.cn.xcszl.cn http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.trtdg.cn.gov.cn.trtdg.cn http://www.morning.zdhnm.cn.gov.cn.zdhnm.cn http://www.morning.ygkk.cn.gov.cn.ygkk.cn http://www.morning.grtwn.cn.gov.cn.grtwn.cn http://www.morning.kpzbf.cn.gov.cn.kpzbf.cn http://www.morning.hlnrj.cn.gov.cn.hlnrj.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.rmmz.cn.gov.cn.rmmz.cn http://www.morning.lbpfl.cn.gov.cn.lbpfl.cn http://www.morning.nbgfz.cn.gov.cn.nbgfz.cn http://www.morning.rlkgc.cn.gov.cn.rlkgc.cn http://www.morning.mbqyl.cn.gov.cn.mbqyl.cn http://www.morning.bchfp.cn.gov.cn.bchfp.cn http://www.morning.hjwkq.cn.gov.cn.hjwkq.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn