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" ...
随机推荐
- [洛谷p2858] 奶牛零食
题目链接: 点我 题目分析: 这是什么,区间dp吗?怎么大佬都在说区间dp的样子 完蛋区间dp都不知道是啥quq 于是使用了玄学的姿势A过了这道题 设dp[i][j][0]表示第i天,左边选了j个,当 ...
- 喵哈哈村的魔法考试 Round #5 (Div.2) ABCC2
官方题解:http://www.cnblogs.com/qscqesze/p/6516139.html 喵哈哈村的狼人杀大战(1) 描述 喵哈哈村最近热衷于玩一个叫做狼人杀的游戏! 张小田今天她抽到的 ...
- 金蝶Apusic中间件适配JetSpeed2过程记录
金蝶Apusic中间件适配JetSpeed2过程记录: 1.安装金蝶并配置域,确保域运行正常. 2.参考<JetSpeed2部署至Apusic操作步骤记录>进行应用迁移. https:// ...
- 阿里Canal框架(数据同步中间件)初步实践
最近在工作中需要处理一些大数据量同步的场景,正好运用到了canal这款数据库中间件,因此特意花了点时间来进行该中间件的的学习和总结. 背景介绍 早期,阿里巴巴B2B公司因为存在杭州和美国双机房部署,存 ...
- pageHelper分页插件失效问题
在bootstrap中引用pageHelper进行页面分页<dependency><groupId>com.github.pagehelper</groupId>& ...
- 简要记录下localStorage在项目中的应用之一
localStorage作为HTML5本地存储web storage特性的API之一,主要作用是将数据保存在客户端中.localStorage保存的数据,一般情况下是永久保存的,也就是说只要采用loc ...
- react基础语法(二)常用语法如:样式 ,自定义属性,常用表达式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- nodejs,python,sublime和Eclipse的包管理器
Python的包管理器叫pip. 首先安装Python运行环境Python 3.7.0:https://www.python.org/downloads/release/python-370/ Pyt ...
- iOS 如何使用TabbarController
xcode中给我内置很多app模版,不过很多时候我们需要更加灵活的初始化项目.下面我就简单介绍一下,如何从0开始制作一个tabbar app. 创建个项目,由于我们从头开始写程序,因此理论上对模版没有 ...
- MySql数据库--持续记录ing
1 基本,引擎,数据类型,运算1.1 基本操作启动:net start mysql停止:net stop mysql连接: mysql –uroot -h127.0.0.1 -proot断开连接:qu ...