LeetCode(11) Container With Most Water
题目
Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.
分析
题意是有个高度数组,就相当于隔板的高度,求数组中任意两隔板间盛水的最大量。隔板间的距离与较低隔
板的高度乘积即为盛水的容量。
AC代码
#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int maxArea(vector<int>& height) {
if (height.empty())
return 0;
int maxCapacity = 0;
size_t lhs = 0, rhs = height.size() - 1;
while (lhs < rhs)
{
int capacity = (rhs - lhs) * min(height[lhs], height[rhs]);
if (capacity > maxCapacity)
{
maxCapacity = capacity;
}
if (height[lhs] < height[rhs])
++lhs;
else
--rhs;
}//while
return maxCapacity;
}
};
int main()
{
Solution s;
vector<int> v = { 1, 2, 3, 4 };
cout << s.maxArea(v) << endl;
system("pause");
return 0;
}
LeetCode(11) Container With Most Water的更多相关文章
- LeetCode(11)Container With Most Water
题目如下: 题目的意思是求容器能装的最大的水量,当时我按梯形的面积来算,一直不对,后来才发现要按矩形的面积来算 Python代码如下: def maxArea(self, height): " ...
- leetcode第11题--Container With Most Water
Problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...
- LeetCode(11):盛最多水的容器
Medium! 题目描述: 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, ...
- Leetcode(11)-盛最多水的容器
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线,使得 ...
- KineticJS教程(11)
KineticJS教程(11) 作者: ysm 11.对象的上下关系 11.1.层的上下关系 Kinetic的层是按照添加到舞台的次序,由下向上排列,上层遮盖下层的图形.每个层各自有一个ZIndex编 ...
- SpringBoot学习笔记(11):使用WebSocket构建交互式Web应用程序
SpringBoot学习笔记(11):使用WebSocket构建交互式Web应用程序 快速开始 本指南将引导您完成创建“hello world”应用程序的过程,该应用程序在浏览器和服务器之间来回发送消 ...
- Leetcode(4)寻找两个有序数组的中位数
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O( ...
- Leetcode(5)最长回文子串
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...
- Leetcode(6)Z字形变换
Leetcode(6)Z字形变换 [题目表述]: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ...
随机推荐
- springMVC的架构与执行流程
SpringMVC术语 前端控制器(DispatcherServlet):接收请求,响应结果,相当于电脑的CPU. 处理器映射器(HandlerMapping):根据URL去查找处理器 处理器(Han ...
- 转 google 制作 邀请函
http://blog.csdn.net/adali/article/details/8315619
- C# 对象复制
/// <summary> /// 把DataTable对象转成List<T>对象 /// </summary> /// <typeparam name=&q ...
- (026)[工具软件]剪切板管理:Ditto
剪切板管理软件:Ditto官网:http://ditto-cp.sourceforge.net/
- DNS练习之反向解析
环境同正向解析一样. 切换到/var/named/chroot/etc目录下: 编辑named.rfc1912.zones文件,在末尾添加如下内容: [root@sishen63 etc]# vim ...
- AJPFX关于Set接口学习笔记及总结
Set接口中的方法和Collection中方法一致的.Set接口取出方式只有一种,迭代器. |--HashSet:底层数据结构是哈希表,线程是不同步的.无序,高效: HashSet集合保证元素唯一性: ...
- 微信小程序 可用性一览
1. 调试vConsole微信小程序通过vConsole的形式观察控制台打印.打印到vConsole控制台的是由 JSON 转化的字符串.这还是可以起到调试作用的. Source Map当es6 转 ...
- QT 学习笔记概述
以下笔记为在看书和实践的过程中的部分记录总结: 0. 窗口布局 1) 支持绝对布局和布局管理器布局; 2) 绝对布局不够灵活.无法自动调整大小,需要手动编写代码调整: 3) 布局管理器管理布局比较灵活 ...
- OpenJudge_2757:最长上升子序列
描述一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对于给定的一个序列(a1, a2, ..., aN),我们可以得到一些上升的子序列(ai1 ...
- uva12105 Bigger is Better
更简单的做法:定义状态dp[i][j]表示在已经用了i根火柴的情况下拼出来了剩余部分(是剩余部分,不是已经拼出来了的)为j(需要%m)的最大长度,一个辅助数组p[i][j]表示状态[i][j]的最高位 ...