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的更多相关文章

  1. LeetCode OJ 238. Product of Array Except Self 解题报告

        题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...

  2. [LeetCode] 11. Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  3. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  4. LeetCode 11 Container With Most Water(分支​判断问题)

    题目链接 https://leetcode.com/problems/container-with-most-water/?tab=Description   Problem: 已知n条垂直于x轴的线 ...

  5. LeetCode 11. Container With Most Water (装最多水的容器)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  6. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  7. LeetCode#11. Container With Most Water

    问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

  8. Java [leetcode 11] Container With Most Water

    问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

  9. C#解leetcode 11. Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

随机推荐

  1. Ansible_Day1

    1.传统运维&自动化运维概念 1)传统的运维概念(硬件.软件.系统.网络) 手工安装系统.机房建设: 软件服务配置.部署通过手工的操作: 没有自动化脚本.流程: 依靠大量的运维人员完成任务: ...

  2. websocket实现心跳连接

    在使用websocket的时候,遇到了一个websocket在连接一段时间就异常断开连接了.第一想法就是重新去连接websocket(websock.onopen),后来发现这种方式是错误的,查阅文档 ...

  3. Java精通并发-锁粗化与锁消除技术实例演示与分析

    在上一次https://www.cnblogs.com/webor2006/p/11446473.html中对锁的升级进行了一个比较详细的理论化的学习,先回忆一下: 编译器对于锁的优化措施: 锁消除技 ...

  4. P2261 [CQOI2007]余数求和[整除分块]

    题目大意 给出正整数 n 和 k 计算 \(G(n, k)=k\ \bmod\ 1 + k\ \bmod\ 2 + k\ \bmod\ 3 + \cdots + k\ \bmod\ n\) 的值 其中 ...

  5. CH6303 天天爱跑步

    6303 天天爱跑步 0x60「图论」例题 描述 小C同学认为跑步非常有趣,于是决定制作一款叫作<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成 ...

  6. 软件测试过程中如何区分什么是功能bug,什么是需求bug,什么是设计bug?

    问题描述: 测试过程中如何区分什么是功能bug,什么是需求bug,什么是设计bug? 精彩答案: 会员 土土的豆豆: 本期问题其实主要是针对不同方面或纬度上对于bug的一个归类和定位. 个人认为,从软 ...

  7. 编程判断输入的字符是否为‘y’或‘Y’,若是,则输出‘yes’,否则输出‘no’

    #include<stdio.h>void main(){ char ch; ch=getchar(); ch == 'y' || ch == 'Y' ? printf("yes ...

  8. 微信网站防屏蔽防红的措施以及微信域名检测API等工具的技术原理

    为什么关心这种技术?因为我经常听到身边搞微商.搞微信项目的朋友都在叫苦连天,由于微信域名屏蔽.微信域名被拦截.弄得他们尸横遍野,损失的连过年回家的路费都没了,曾经的叱咤风云一下变成了今日的倒亏损.腾讯 ...

  9. Kafka kSQL sql查询

    背景 kafka早期作为一个日志消息系统,很受运维欢迎的,配合ELK玩起来很happy,在kafka慢慢的转向流式平台的过程中,开发也慢慢介入了,一些业务系统也开始和kafka对接起来了,也还是很受大 ...

  10. OPCode详解及汇编与反汇编原理

    1. 何为OPCode 在计算机科学领域中,操作码(Operation Code, OPCode)被用于描述机器语言指令中,指定要执行某种操作的那部分机器码,构成OPCode的指令格式和规范由处理器的 ...