【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 ...
随机推荐
- cmake学习之-configure_file
一.系统版本 cmake version: 3.5.2 系统版本: Ubuntun 16.04 cmake docment: 3.14.4 最后更新: 2019-05-30 二.指令说明 config ...
- 设计一个线程安全的单例(Singleton)模式
在设计单例模式的时候.尽管非常easy设计出符合单例模式原则的类类型,可是考虑到垃圾回收机制以及线程安全性.须要我们思考的很多其它.有些设计尽管能够勉强满足项目要求,可是在进行多线程设计的时候.不考虑 ...
- Spring Cloud Zuul API服务网关之请求路由
目录 一.Zuul 介绍 二.构建Spring Cloud Zuul网关 构建网关 请求路由 请求过滤 三.路由详解 一.Zuul 介绍 通过前几篇文章的介绍,我们了解了Spring Cloud ...
- 开源监控软件ganglia
开源监控软件ganglia安装手册 Ganglia是一个监控服务器,集群的开源软件,能够用曲线图表现最近一个小时,最近一天,最近一周,最近一月,最近一年的服务器或者集群的cpu负载,内存,网络,硬盘等 ...
- charles 4.x 破解版安装 以及使用
下载地址 https://pan.baidu.com/s/1dFvYM7B 破解方法 未破解的情况下,每30分钟会弹出一个提示,然后关闭软件 将压缩包内的 charles.jar 复制到安装目录下,替 ...
- 目标检测之人头检测(HaarLike Adaboost)---高密度环境下行人检测和统计
实验程序视频 下载 1 问题描述 高密度环境下的行人统计一直没有得到很好的解决,主要原因是对高密度人群中的行人检测和跟踪是一个很难的问题,如下图所示环境,存在的困难包括: 检测方面: 由于人群整体处于 ...
- Android 超高仿微信图片选择器 图片该这么载入
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/39943731,本文出自:[张鸿洋的博客] 1.概述 关于手机图片载入器,在当今像 ...
- TP框架---thinkphp查询和添加数据
查询 <?php namespace Admin\Controller; use Think\Controller; class MainController extends Controlle ...
- EasyDSS RTMP流媒体服务器web前端:vue组件之间的传值,父组件向子组件传值
之前接触最多的都是EasyNVR,主要针对的都是前端的一些问题.也有接触到一些easydss流媒体服务器. 前端方面的,EasyDSS流媒体服务器与EasyNVR有着根本的不同.EasyNVR使用的是 ...
- redis启动错误-- Creating Server TCP listening socket *:6379: listen: UnKnown error
前提:windows server 2008.redis 3.x 今天给服务器部署redis环境,文件配置.服务安装都很顺利,可就在启动服务的时候提示 百度老半天也没找到个说到点子上的. 这里记录下解 ...