LeetCode_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.
class Solution {
public:
int maxArea(vector<int> &height) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int size = height.size();
if(size < ) return ;
int maxA = ,temp;
int left = , right = size -; while(left < right)
{
if( height[left] <height[right] )
{
temp = height[left] * (right - left);
left ++;
}else{ temp = height[right] * (right - left ) ;
right--;
} maxA = maxA > temp ? maxA : temp ; } return maxA ;
}
};
贪心的思想。
最近发现自己越来越搓了,要努力了!
LeetCode_Container With Most Water的更多相关文章
- [LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- [LeetCode] Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- [LeetCode] Water and Jug Problem 水罐问题
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- 【leetcode】Container With Most Water
题目描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- [LintCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LintCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
随机推荐
- 单元测试之C/C++
如今TDD很火,我公司小,一般写代码不写测试用例的,一般就是随便测试下函数的输入输出,没用工具或框架来测试,非常简单,一点也不正规化. 在一种传统的结构化编程语言中,比如C,要进行测试的单元一般是函数 ...
- penetration testers渗透测试,hack,vnc,nat,
penetration testers渗透测试,hack,vnc,nat,
- BZOJ3016: [Usaco2012 Nov]Clumsy Cows
3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 71 Solved: 52[Submi ...
- strcat函数的坑点
我们先看下面这样一段代码: #include <iostream> #include <stdlib.h> using namespace std; int main() { ...
- yaml 1.6 操作
/** * Copyright (c) 2008, http://www.snakeyaml.org * * Licensed under the Apache License, Version 2. ...
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile 解决办法
Maven install失败 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (d ...
- Eclipse调试时出现source not found的问题
Eclipse中新加载了一个java项目,打断点debug时,Debug小窗口显示source not found,网搜找到了解决办法,在此记录一下. 原文地址:http://blog.csdn.ne ...
- Eclipse+Java+OpenCV246人脸识别
1.环境搭建:见上一篇博客 整个项目的结构图: 2.编写DetectFaceDemo.java,代码如下: package com.njupt.zhb.test; import org.opencv. ...
- [WebStrom] Cannot detect file change to trigger webpack re-compile
Working with editors/IDEs supporting “safe write” Note that many editors support “safe write” featur ...
- webform开发经验(一):Asp.Net获取Checkbox选中的值
webform中获取repeat控件列表下的checkbox选中的值: 码农上代码: public static string getSelectedIDs(Repeater Rpt_) { stri ...