leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water
https://www.cnblogs.com/grandyang/p/4455109.html
用双指针向中间滑动,较小的高度就作为当前情况的高度,然后循环找容量的最大值。
不管两个指针中间有多少高度的柱子,只管两头,因为两头的才决定最大容量。
class Solution {
public:
int maxArea(vector<int>& height) {
if(height.empty())
return ;
int res = ;
int begin = ,end = height.size() - ;
while(begin < end){
int h = min(height[begin],height[end]);
res = max(res,(end - begin) * h);
h == height[begin] ? begin++ : end --;
}
return res;
}
};
42. Trapping Rain Water
https://www.cnblogs.com/grandyang/p/4402392.html
本题与11. Container With Most Water不同,11. Container With Most Water是求两个柱子能装的最多水量,这个题是求的所有柱子能装的水量。
依旧可以使用双指针,一个begin指向第一个柱子,一个end指向最后一个柱子。然后相当于维护一个递减的队列,但又不是完全递减,只是说后面遍历到的应该比这个开始的位置少。一旦出现比这个开始位置大,就要重新更新作为比较的对象。
注意,选择对比的是两个柱子中较短的那根柱子。
class Solution {
public:
int trap(vector<int>& height) {
int begin = ,end = height.size() - ;
int res = ;
while(begin < end){
int h = min(height[begin],height[end]);
if(h == height[begin]){
int tmp = height[begin];
begin++;
while(begin < end && height[begin] < tmp){
res += tmp - height[begin];
begin++;
}
}
else{
int tmp = height[end];
end--;
while(begin < end && height[end] < tmp){
res += tmp - height[end];
end--;
}
}
}
return res;
}
};
238. Product of Array Except Self
https://www.cnblogs.com/grandyang/p/4650187.html
整体分成左右两个数组进行相乘,这种方式不用两个数组进行存储。
从左向右可以用res[i]就代替前面的乘积,但从右向左就不行了,这个是后res[i]已经是所有的在左侧前方的乘积和,我们还必须计算右侧的乘积和,这个时候用一个变量right来累乘就好了。
其实从左向右也可以用一个变量来累乘。
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> res(nums.size(),);
for(int i = ;i < nums.size();i++){
res[i] = res[i-] * nums[i-];
}
int right = ;
for(int i = nums.size() - ;i >= ;i--){
res[i] *= right;
right *= nums[i];
}
return res;
}
};
407. Trapping Rain Water II
https://www.cnblogs.com/grandyang/p/5928987.html
class Solution {
public:
int trapRainWater(vector<vector<int>>& heightMap) {
if(heightMap.empty())
return ;
int m = heightMap.size(),n = heightMap[].size();
int res = ,max_height = INT_MIN;
vector<vector<bool>> visited(m,vector<bool>(n,false));
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
for(int i = ;i < m;i++){
for(int j = ;j < n;j++){
if(i == || i == m - || j == || j == n - ){
int index = i * n + j;
q.push(make_pair(heightMap[i][j],index));
visited[i][j] = true;
}
}
}
while(!q.empty()){
int height = q.top().first;
int x = q.top().second / n;
int y = q.top().second % n;
q.pop();
max_height = max(max_height,height);
for(auto dir : dirs){
int new_x = x + dir[];
int new_y = y + dir[];
if(new_x < || new_x >= m || new_y < || new_y >= n || visited[new_x][new_y] == true)
continue;
visited[new_x][new_y] = true;
if(heightMap[new_x][new_y] < max_height)
res += max_height - heightMap[new_x][new_y];
int new_index = new_x * n + new_y;
q.push(make_pair(heightMap[new_x][new_y],new_index));
}
}
return res;
}
private:
vector<vector<int>> dirs{{-,},{,-},{,},{,}};
};
leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II的更多相关文章
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- [LeetCode] 11. 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(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- LeetCode 11 Container With Most Water(分支判断问题)
题目链接 https://leetcode.com/problems/container-with-most-water/?tab=Description Problem: 已知n条垂直于x轴的线 ...
- LeetCode 11. 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#11. Container With Most Water
问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- Java [leetcode 11] Container With Most Water
问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- C#解leetcode 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
随机推荐
- 安装配置KVM虚拟化
安装KVM虚拟化 KVM需要硬件⽀持, 所以需要开启虚拟化⽀持 硬件设备直接在BIOS设置开启CPU虚拟化 个⼈电脑同样进⼊BIOS开启虚拟化⽀持 VM需要找到对应虚拟机开启对应的VT-EPT虚拟化技 ...
- c++第五次作业
运算符重载 一.运算符重载基本规则 1.运算符重载是对已有的运算符赋予多重含义,使同一个运算符作用于不同类型的数据时导致不同的行为. 2.一般来讲,重载的功能应当与原有功能相似,不能改变原运算符的操作 ...
- abp学习(四)——根据入门教程(aspnetMVC Web API进一步学习)
Introduction With AspNet MVC Web API EntityFramework and AngularJS 地址:https://aspnetboilerplate.com/ ...
- k8s删除pod时,docker服务出现挂载点泄漏问题的解决
k8s更新版本后,老的POD一直出现Terminating,多久都不能删除. 然后,进入具体的节点机器之后,查看日志输出如下类似: ERROR: driver "overlay" ...
- 题解 洛谷P4779 【【模板】单源最短路径(标准版)】
正权图,貌似看来是一道裸的 \(dijkstra\) \(dijkstra\)的主要步骤: 首先,在\(dijkstra\)中,源点表示一开始的出发点,蓝点表示还未确定的点,白点则表示已经确定的点. ...
- canvans知识点
1.绘制圆的角度示意图: 2 倒计时中,时钟数字的渲染逻辑: 3 直线边缘样式的设置 context.lineCap = "butt"; context.lineCap = &qu ...
- test20190814 NOIP2019 模拟题
二叉树 [问题描述] 从前有一棵二叉树,我们用如下方式来表示这棵二叉树. 如果一个节点没有儿子,我们用"0"来表示他. 如果一个节点有一个儿子,我们对它的表示以"1&qu ...
- laravel5.8 编译laravel mix
如果第一次无需执行(如果编译的时候出错再次执行才需要) 1:rm -rf node_modules 更改镜像为淘宝镜像 2:yarn config set registry https://regis ...
- [React] Write a Custom State Hook in React
Writing your own custom State Hook is not as a daunting as you think. To keep things simple, we'll r ...
- Kubernetes 学习18配置网络插件flannel
一.概述 1.我们在学习docker时知道docker有四种常用的网络模型 a.bridge:桥接式网络 b.joined:联盟式网络,共享使用另外一个容器的网络名称空间 b.opened:容器直接共 ...