【LeetCode】011 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.
题解:
暴力解即可,注意这个面积是长方形的,我刚开始犯傻给写成梯形了。。。两个思路:两个for循环遍历,结果就是TLE了。另一个就是头尾指针遍历法,时间复杂度降为O(n).以后遇到这种需要遍历的,要先想到头尾指针法,而不是两个for循环(其实也是双指针,只是位置不一样)。同时,与之前遇到的数组问题一样,小幅度的优化就是在大循环内就跳过重复项
Solution 1 (TLE)
class Solution {
public:
int maxArea(vector<int>& height) {
int area = , n = height.size();
for(int i=; i<n-; i++) {
for(int j=i+; j<n; j++) {
int new_area = (j-i)*min(height[i], height[j]);
if(height[i]== || height[j]==) new_area = ;
area = max(area, new_area);
}
}
return area;
}
};
Solution 2
class Solution {
public:
int maxArea(vector<int>& height) {
int area = , i = , j = height.size() - ;
while (i < j) {
area = max(area, min(height[i], height[j]) * (j - i));
height[i] < height[j] ? ++i : --j;
}
return area;
}
};
Solution 3
class Solution {
public:
int maxArea(vector<int>& height) {
int area = ;
int i = , j = height.size() - ;
while (i < j) {
int h = min(height[i], height[j]);
area = max(area, (j - i) * h);
while (height[i] <= h && i < j) i++;
while (height[j] <= h && i < j) j--;
}
return area;
}
};
【LeetCode】011 Container With Most Water的更多相关文章
- 【LeetCode】11. Container With Most Water 盛最多水的容器
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...
- 【LeetCode】11. Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- 【LeetCode】778. Swim in Rising Water 水位上升的泳池中游泳(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/swim-in- ...
- 【leetcode】778. Swim in Rising Water
题目如下: 解题思路:本题题干中提到了一个非常重要的前提:"You can swim infinite distance in zero time",同时也给了一个干扰条件,那就是 ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- leetcode面试准备:Container With Most Water
leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
随机推荐
- Scrapy安装向导
原文地址 https://doc.scrapy.org/en/latest/intro/install.html 安装Scrapy Scrapy运行在python2.7和python3.3或以上版本( ...
- Lua数据库访问
© 版权声明:本文为博主原创文章,转载请注明出处 1.代码 luasql = require "luasql.mysql" --创建环境对象 env = luasql.mysql( ...
- flash插件使用外部数据的方法
使用xml保存需要改变的数据,如轮播图的图片路径,也可以在xml中指定数据库地址等
- 开始nodejs+express的学习+实践(1)
开始nodejs+express的学习+实践(1) 开始nodejs+express的学习+实践(2) 开始nodejs+express的学习+实践(3) 开始nodejs+express的学习+实践 ...
- 【demo练习三】:图片水平滚动、点击按钮变更图片动画
要求:四张图片水平滚动,每隔5秒进行一次循环,点击按钮随机变更图片. XAML前台代码: <Window x:Class="图片滚动.MainWindow" xmlns=&q ...
- php中的字符串和正則表達式
一.字符串类型的特点 1.PHP是弱类型语言,其它数据类型一般都能够直接应用于字符串函数操作. 1: <? php 2: echo substr("123456",2,4); ...
- 【Unity 3D】学习笔记三十:游戏元素——游戏地形
游戏地形 在游戏的世界中,必然会有非常多丰富多彩的游戏元素融合当中. 它们种类繁多.作用也不大同样.一般对于游戏元素可分为两种:经经常使用.不经经常使用.经常使用的元素是游戏中比較重要的元素.一般须要 ...
- lnmp建站常识
1.nginx配置网站目录并修改访问的端口:nginx.conf文件 listen 666;//端口默认为80,修改后增强安全性 server_name www.lnmp.org; index ind ...
- MySQL中使用INNER JOIN来实现Intersect并集操作
MySQL中使用INNER JOIN来实现Intersect并集操作 一.业务背景 我们有张表设计例如以下: CREATE TABLE `user_defined_value` ( `RESOURCE ...
- 【BZOJ2666】[cqoi2012]组装 贪心
[BZOJ2666][cqoi2012]组装 Description 数轴上有m个生产车间可以生产零件.一共有n种零件,编号为1~n.第i个车间的坐标为xi,生产第pi种零件(1<=pi< ...