leetcode笔记--水箱问题
类型的引用:Solution *s=new Solution();
1.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 and n is at least 2.
给定一个vector作为纵线的高度输入,选择两条纵线使得他们和X轴一起构成的容器能够盛更多水。
参考:http://blog.csdn.net/patkritlee/article/details/52453417
解析:假设最大容器两条纵线为i,j,那么:a.在j的右端没有一条线会比它高!
b.在i的左端也不会有比它高的线!
c.所以我们从两头向中间靠拢,同时更新候选值;在收拢区间的时候优先从x,y中较小的边开始收缩;
代码:
class Solution {
public:
int maxArea(vector<int>& height) {
int l=0;
int r=height.size()-1;
int area=0;
while(l<r){
area=max(area,(r-l)*min(height[r],height[l]));
//查找l,r之间较小的h,从较小的一端开始逼近,找中间比它高的数值。
if(height[l]<height[r]){
int k=l;
while(height[k]<=height[l])
k++;
l=k;
}
else{
int k=r;
while(height[k]<=height[r])
k--;
r=k;
}
}
return area;
}
};
leetcode笔记--水箱问题的更多相关文章
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- Leetcode 笔记 35 - Valid Soduko
题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...
随机推荐
- MQ消息队列之MSMQ
主要参考文章: 消息队列(Message Queue)简介及其使用
- ANGULAR6.x - 错误随笔 - Can't bind to 'formGroup'
formGroup:错误 Can't bind to 'formGroup' since it isn't a known property of 'form'. (" 原因: 在使用for ...
- 开源堡垒机Gateone 安装过程记录及报错处理
1.下载git源码或者zip包都可以,下载到我们制定部署目录. git地址:https://github.com/liftoff/GateOne.git 2.检查tornado 版本,安装tornad ...
- HUE的自动化安装部署
HUE=Hadoop User Experience(Hadoop用户体验),直白来说就一个开源的Apache Hadoop UI系统,由Cloudera Desktop演化而来,最后Cloudera ...
- git branch & checkout fetch 的使用和冲突解决
git branch & checkout fetch 的使用和冲突解决 branch git branch 查看本地分支 git branch -v 查看本地分支的具体信息(commi ...
- HDFS常用API(2)
一.读取HDFS文件数据.将本地文件写入HDFS中文件.使用IOUtils读写数据 ** * @author: PrincessHug * @date: 2019/3/18, 17:24 * @Blo ...
- python网站开发准备ubuntu14.04安装mysql实现windows管理
sudo apt-get install mysql-server mysql-client 输入root密码 然后确认安装tab选定确认 输入数据库密码 重复输入 启动 sudo service m ...
- String,StringBuffer与StringBuilder的区别|线程安全与线程不安全
https://www.cnblogs.com/xingzc/p/6277581.html
- React-Native + Genymotion android开发环境搭建
1.解压android-sdk_r24.3.4-windows.zip放到一个空间大的开发盘中 2.添加环境变量,路径时 ANDROID_HOME D:\Android\android-sdk-win ...
- LR特征维数特别大实时计算问题
美团 https://tech.meituan.com/machinelearning-data-feature-process.html 维数灾难 待续...