11:盛最多水的容器
This commit is contained in:
parent
4a394d5574
commit
262c1663c0
87
src/main/java/leetcode/editor/cn/ContainerWithMostWater.java
Normal file
87
src/main/java/leetcode/editor/cn/ContainerWithMostWater.java
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
//给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i,
|
||||||
|
//ai) 和 (i, 0) 。找出其中的两条线,使得它们与 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
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:height = [4,3,2,1,4]
|
||||||
|
//输出:16
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 4:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:height = [1,2,1]
|
||||||
|
//输出:2
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// n = height.length
|
||||||
|
// 2 <= n <= 3 * 104
|
||||||
|
// 0 <= height[i] <= 3 * 104
|
||||||
|
//
|
||||||
|
// Related Topics 数组 双指针
|
||||||
|
// 👍 2425 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//11:盛最多水的容器
|
||||||
|
public class ContainerWithMostWater {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new ContainerWithMostWater().new Solution();
|
||||||
|
//49
|
||||||
|
System.out.println(solution.maxArea(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7}));
|
||||||
|
//1
|
||||||
|
System.out.println(solution.maxArea(new int[]{1, 1}));
|
||||||
|
//16
|
||||||
|
System.out.println(solution.maxArea(new int[]{4, 3, 2, 1, 4}));
|
||||||
|
//2
|
||||||
|
System.out.println(solution.maxArea(new int[]{1, 2, 1}));
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int maxArea(int[] height) {
|
||||||
|
int start = 0;
|
||||||
|
int end = height.length - 1;
|
||||||
|
int max = 0;
|
||||||
|
while (start < end) {
|
||||||
|
max = Math.max(max, (end - start) * Math.min(height[start], height[end]));
|
||||||
|
if (height[start] < height[end]) {
|
||||||
|
start++;
|
||||||
|
} else {
|
||||||
|
end--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
46
src/main/java/leetcode/editor/cn/ContainerWithMostWater.md
Normal file
46
src/main/java/leetcode/editor/cn/ContainerWithMostWater.md
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<p>给你 <code>n</code> 个非负整数 <code>a<sub>1</sub>,a<sub>2,</sub>...,a</code><sub><code>n</code>,</sub>每个数代表坐标中的一个点 <code>(i, a<sub>i</sub>)</code> 。在坐标内画 <code>n</code> 条垂直线,垂直线 <code>i</code> 的两个端点分别为 <code>(i, a<sub>i</sub>)</code> 和 <code>(i, 0)</code> 。找出其中的两条线,使得它们与 <code>x</code> 轴共同构成的容器可以容纳最多的水。</p>
|
||||||
|
|
||||||
|
<p><strong>说明:</strong>你不能倾斜容器。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<p><img alt="" src="https://aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com/aliyun-lc-upload/uploads/2018/07/25/question_11.jpg" style="height: 287px; width: 600px;" /></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>[1,8,6,2,5,4,8,3,7]
|
||||||
|
<strong>输出:</strong>49
|
||||||
|
<strong>解释:</strong>图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>height = [1,1]
|
||||||
|
<strong>输出:</strong>1
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>height = [4,3,2,1,4]
|
||||||
|
<strong>输出:</strong>16
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 4:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>height = [1,2,1]
|
||||||
|
<strong>输出:</strong>2
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>n = height.length</code></li>
|
||||||
|
<li><code>2 <= n <= 3 * 10<sup>4</sup></code></li>
|
||||||
|
<li><code>0 <= height[i] <= 3 * 10<sup>4</sup></code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>数组</li><li>双指针</li></div></div>\n<div><li>👍 2425</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user