【Container With Most Water】cpp
题目:
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.
代码:
class Solution {
public:
int maxArea(vector<int>& height) {
if ( height.size()< ) return ;
int max_area = ;
int left = ;
int right = height.size()-;
while ( left<right )
{
if ( height[left]<=height[right] )
{
max_area = std::max(max_area, (right-left)*height[left]);
left++;
}
else
{
max_area = std::max(max_area, (right-left)*height[right]);
right--;
}
}
return max_area;
}
};
tips:
试图用DP去做,但是没想出来;最后无奈落入了Greedy的俗套solution。
这个greedy的思路也是蛮巧的:从两头开始往中间greedy,头尾两个greedy一起变化才得到greedy的条件。
=======================================
第二次过这道题,这道题题意不清晰:如果选了某两个板子,就当其他板子不存在。
class Solution {
public:
int maxArea(vector<int>& height) {
int l = ;
int r = height.size()-;
int ret = ;
while ( l<r )
{
if ( height[l]<=height[r] )
{
ret = max(ret, (r-l)*height[l]);
l++;
}
else
{
ret = max(ret, (r-l)*height[r]);
r--;
}
}
return ret;
}
};
【Container With Most Water】cpp的更多相关文章
- leetcode 【 Container With Most Water 】python 实现
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- 【Trapping Rain Water】cpp
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- Hdu 4738【求无向图的桥】.cpp
题目: 曹操在长江上建立了一些点,点之间有一些边连着.如果这些点构成的无向图变成了连通图,那么曹操就无敌了.刘备为了防止曹操变得无敌,就打算去摧毁连接曹操的点的桥.但是诸葛亮把所有炸弹都带走了,只留下 ...
- 【Search a 2D Matrix】cpp
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- 【Search for a Range】cpp
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
- 【Merge K Sorted Lists】cpp
题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- 【Merge Two Sorted Lists】cpp
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- 【Largest Rectangle in Histogram】cpp
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- 【Validate Binary Search Tree】cpp
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
随机推荐
- FusionCharts使用教程:为JavaScript图表提供数据
FusionCharts的JavaScript类提供了一系列的函数来提供图表数据. FusionCharts的JavaScript类支持XML或JSON格式的数据.这些数据可以是URL或字符串. 以X ...
- Linux 下查找指令
原文链接:http://www.cnblogs.com/sunleecn/archive/2011/11/01/2232210.html whereis <程序名称>查找软件的安装路径-b ...
- 【Android开发笔记】底部菜单栏 FragmentTabHost
公司项目,需求本来是按照谷歌官方指南写的,菜单栏设计成在导航栏下方 结果呢,审评时,BOSS为了和iOS统一,改成了底部菜单栏(标准结局),我只能呵呵呵呵呵呵呵 查了查资料发现实现底部菜单栏用的是Fr ...
- FreeBSD 安裝 wget
cd /usr/ports/ftp/wgetmake install clean pkg_add -r wget就可以把wget安装上去了
- Redis集群维护、运营的相关命令与工具介绍
Redis集群的搭建.维护.运营的相关命令与工具介绍 一.概述 此教程主要介绍redis集群的搭建(Linux),集群命令的使用,redis-trib.rb工具的使用,此工具是ruby语言写的,用于集 ...
- 数据字典的设计--3.首页添加删除表格(JS实现)
页面效果: JS代码: 1.添加表格 function insertRows(){ //获取表格对象 var tb1 = $("#dictTbl"); var tempRow = ...
- springMvc-reset风格和对静态资源的管理
1.所谓rest风格及比较优雅的,没有一大堆后缀的风格 2.对静态资源的管理,及样式.图片等不需要springMvc过滤 代码: 1.在springMvc的配置文件中添加mvc标签 <?xml ...
- cms-幻灯片的实现
1.其实幻灯片的后台代码和之前的最新动态和推荐是一样的,只是前台遍历的时候不一样罢了 2.代码: 2.1:帖子mapper查询出幻灯片图片: <?xml version="1.0&qu ...
- js 获取时间戳 登陆验证码生成要加时间戳
JavaScript 获取当前时间戳,登陆验证码生成要加时间戳,防止存在session不重新请求第一种方法: var timestamp = Date.parse(new Date()); 结果:12 ...
- hbase查询基于标准sql规范中间件Phoenix
Phoenix是个很好的hbase 查询工具,在hbase中安装也很简单,可以按照 http://www.cnblogs.com/laov/p/4137136.html 这个连接中进行配置客户端和服务 ...