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 ...
随机推荐
- Hankson 的趣味题
题解: 硬是把一道傻逼题写到了200行.. 长长的模板就有70行.. 由于我没有做的时候觉得并不保证$a1|a0$ $b0|b1$ 然后就加了很多特判.. 我的做法就是暴力分解质因数 T*sqrt(n ...
- 【BZOJ】3730: 震波
原题链接 题解 查询距离一个点距离在一定范围内的点,直接点分树,前缀和用树状数组维护 答案是当前重心距离不超过k - (x到重心距离)的点的前缀和,减去在x所在子树中,距离重心不超过k - (x到重心 ...
- 如何在grails2.3.x中的fork模式下进行调试?-【grails】
grails2.3.x中默认情况下运行模式被设置成了fork模式,在这种模式下,大家会发现设置了断点后无法进行中断.这是由于fork模式造成的,因为在fork模式下,JVM新起了一个进程,这样调试器就 ...
- BZOJ3451 Tyvj1953 Normal 点分治 多项式 FFT
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ3451.html 题目传送门 - BZOJ3451 题意 给定一棵有 $n$ 个节点的树,在树上随机点分 ...
- Spring日记_02之 json、javaBean、.do、MySql、MyBatis 环境搭建结束
JSON Json是JavaScript直接量语法 无参构造方法直接 Alt + \ 就可以提示添加 Project – Clean 浏览器向服务器发送请求,服务器中的Spring中的SpringMV ...
- day75 form 组件(对form表单进行输入值校验的一种方式)
我们的组件是什么呢 select distinct(id,title,price) from book ORM: model.py class Book(): title=model.CharFiel ...
- day56 文件 文档处理,事件
前情回顾: 1. 前情回顾 0. 选择器补充 - 属性选择器 - $("[egon]") - $("[type='text']") - $("inpu ...
- day32 process模块用法
昨日作业: 服务端: 服务端: from socket import * from multiprocessing import Process def server(ip,port): server ...
- radio按钮单选效果
必须有name,并且是同一值,判断效果可用value值确定
- 初窥Java之四
一.条件判断之if判断 语法格式:if(结果为布尔类型的结果值){ 功能执行语句; }else if(结果为布尔类型的结果值){ 功能执行语句; } ....{ }else{ 功能执行语句: } 注意 ...