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 ...
随机推荐
- Ncurses - Panel
当你需要创建许多窗口时,你很快就会发现它们会变得难以管理.Panel library提供了很好的解决方案. Panel 实际上是一个窗口,通过容器 - 栈 来管理,栈顶的 panel 是完全可见的,其 ...
- 【bzoj1042】[HAOI2008]硬币购物 背包dp+容斥原理
题解: 计数题 首先考虑容斥 这题很明显加了限制状态就很多 考虑没有限制 显然可以直接dp 然后 我们看一下 容斥 某一个使用>=k张 那么其实就是 f[i-k*c[]] 于是这样就可以做了
- Orchard是如何工作的?
文章翻译自http://docs.orchardproject.net/Documentation/How-Orchard-works 对Orchard的理解还不深刻,翻译可能有不好的地方. ...
- appium---第一个脚本--启动一个已存在的app
1.可以使用android-sdk中的aapt工具 ①.选择一个版本的build_tools,加入path环境变量中 ②.验证aapt环境是否正常 3.下载你要测试的包,放入某一地址中(随意):aap ...
- String.getBytes()和String.tocharArray(),字节数组和字符数组的区别
String.getBytes()是将字符串转化为一个字节数组.而String.toCharArray()是将一个字符串转化为一个字符数组. [例如] byte bys[] ="国庆60周年 ...
- 简单的使用Nginx框架搭建Web服务器~
系统环境Debian 8,内核版本 一.首先来安装nginx服务程序: 1.安装nginx服务需要的相关程序(记得在root权限下操作下面的指令) aptitude install libpcre3 ...
- 初窥Java之一
一.常用的dos命令 打开命令提示符窗口的方式: ① win + R --> 输入cmd --> 回车 ② 开始 --> 搜索程序和文件的框中输入 cmd --> 回车 ③ ...
- TF:利用TF的train.Saver将训练好的variables(W、b)保存到指定的index、meda文件—Jason niu
import tensorflow as tf import numpy as np W = tf.Variable([[2,1,8],[1,2,5]], dtype=tf.float32, name ...
- windows下编译php7图形库php_ui.dll
CSDN博客 具有图形化编程才有意思,这几天看到了php ui 图形扩展,只是现在只能下载php 7.1的 本次教程编译php7.2.6的 php ui 要是linux下编译起来比较简单 但是 win ...
- vim编辑器第二天
编辑模式的进入: i :在光标所在的字符前插入 a :在光标所在的字符后插入 o :在光标所在的行的下面一行插入 I : 在光标所在的行的行首插入,如果行首有空格则在空格后面开始插入 A :在光标 ...