【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 ...
随机推荐
- aar格式
aar包是Android Library Project的二进制公布包. 文件的扩展名是aar,并且maven包类型也应该是aar. 只是这文件本身就是一个简单的zip文件.里面有例如以下的内容: / ...
- android 自己定义组件随着手指自己主动画圆
首先自己定义一个View子类: package com.example.androidtest0.myView; import android.content.Context; import andr ...
- Content encoding error问题解决方法
A few people have been experiencing the following error. UPDATE: The reason for it happening is beca ...
- create-react-app创建项目报错SyntaxError: Unexpected end of JSON input while parsing near '...ttachment":false,"tar' npm代理
SyntaxError: Unexpected end of JSON input while parsing near '...ttachment":false,"tar' 错误 ...
- python 基础 7.4 os 模块
#/usr/bin/python #coding=utf8 #@Time :2017/11/11 3:15 #@Auther :liuzhenchuan #@File :os 模块.py im ...
- Python中的注解“@” 、Java 注解
https://blog.csdn.net/u013474436/article/details/75675113 https://blog.csdn.net/briblue/article/deta ...
- stacked generalization 堆积正则化 堆积泛化 加权特征线性堆积
https://en.wikipedia.org/wiki/Ensemble_learning Stacking Stacking (sometimes called stacked generali ...
- PHP上传视频
https://github.com/chaping/plupload_docs 这下载 lupload有以下功能和特点: 1.拥有多种上传方式:HTML5.flash.silverlight以及传统 ...
- TensorFlow框架(4)之CNN卷积神经网络详解
1. 卷积神经网络 1.1 多层前馈神经网络 多层前馈神经网络是指在多层的神经网络中,每层神经元与下一层神经元完全互连,神经元之间不存在同层连接,也不存在跨层连接的情况,如图 11所示. 图 11 对 ...
- webpack为什么加载不了css?
原文地址: https://segmentfault.com/q/1010000005099261 这个app是用react写的. webpack的loader设置是这样的 module:{ load ...