LeetCode 755. Pour Water
原题链接在这里:https://leetcode.com/problems/pour-water/description/
题目:
We are given an elevation map, heights[i]
representing the height of the terrain at that index. The width at each index is 1. After V
units of water fall at index K
, how much water is at each index?
Water first drops at index K
and rests on top of the highest terrain or water at that index. Then, it flows according to the following rules:
- If the droplet would eventually fall by moving left, then move left.
- Otherwise, if the droplet would eventually fall by moving right, then move right.
- Otherwise, rise at it's current position.
Here, "eventually fall" means that the droplet will eventually be at a lower level if it moves in that direction. Also, "level" means the height of the terrain plus any water in that column.
We can assume there's infinitely high terrain on the two sides out of bounds of the array. Also, there could not be partial water being spread out evenly on more than 1 grid block - each unit of water has to be in exactly one block.
Example 1:
Input: heights = [2,1,1,2,1,2,2], V = 4, K = 3
Output: [2,2,2,3,2,2,2]
Explanation:
# #
# #
## # ###
#########
0123456 <- index The first drop of water lands at index K = 3: # #
# w #
## # ###
#########
0123456 When moving left or right, the water can only move to the same level or a lower level.
(By level, we mean the total height of the terrain plus any water in that column.)
Since moving left will eventually make it fall, it moves left.
(A droplet "made to fall" means go to a lower height than it was at previously.) # #
# #
## w# ###
#########
0123456 Since moving left will not make it fall, it stays in place. The next droplet falls: # #
# w #
## w# ###
#########
0123456 Since the new droplet moving left will eventually make it fall, it moves left.
Notice that the droplet still preferred to move left,
even though it could move right (and moving right makes it fall quicker.) # #
# w #
## w# ###
#########
0123456 # #
# #
##ww# ###
#########
0123456 After those steps, the third droplet falls.
Since moving left would not eventually make it fall, it tries to move right.
Since moving right would eventually make it fall, it moves right. # #
# w #
##ww# ###
#########
0123456 # #
# #
##ww#w###
#########
0123456 Finally, the fourth droplet falls.
Since moving left would not eventually make it fall, it tries to move right.
Since moving right would not eventually make it fall, it stays in place: # #
# w #
##ww#w###
#########
0123456 The final answer is [2,2,2,3,2,2,2]: #
#######
#######
0123456
Example 2:
Input: heights = [1,2,3,4], V = 2, K = 2
Output: [2,3,3,4]
Explanation:
The last droplet settles at index 1, since moving further left would not cause it to eventually fall to a lower height.
Example 3:
Input: heights = [3,1,3], V = 5, K = 1
Output: [4,4,4]
Note:
heights
will have length in[1, 100]
and contain integers in[0, 99]
.V
will be in range[0, 2000]
.K
will be in range[0, heights.length - 1]
.
题解:
根据题目模拟过程,每次找出适合防水的index, 让后把此处高度+1. 重复水量次数.
Time Complexity: O(V*n). n = heights.length.
Space: O(1).
AC Java:
class Solution {
public int[] pourWater(int[] heights, int V, int K) {
if(heights == null || K >= heights.length){
return heights;
} while(V-->0){
int index = findPosition(heights, K);
if(index>=0 && index<heights.length){
heights[index]++;
}
} return heights;
} private int findPosition(int [] nums, int start){
int res = start;
int l = start;
int r = start; while(r<nums.length-1 && nums[r+1]<=nums[r]){
if(nums[r+1]<nums[r]){
res = r+1;
}
r++;
} while(l>=1 && nums[l-1]<=nums[l]){
if(nums[l-1]<nums[l]){
res = l-1;
}
l--;
} return res;
}
}
LeetCode 755. Pour Water的更多相关文章
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【LeetCode】365. Water and Jug Problem 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学题 相似题目 参考资料 日期 题目地址:http ...
- ZOJ 3913 Bob wants to pour water ZOJ Monthly, October 2015 - H
Bob wants to pour water Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge There i ...
- 【leetcode】365. Water and Jug Problem
题目描述: You are given two jugs with capacities x and y litres. There is an infinite amount of water su ...
- LintCode——Pour Water
Pour Water: We are given an elevation map, heights[i] representing the height of the terrain at that ...
- [LeetCode] Pour Water 倒水
We are given an elevation map, heights[i] representing the height of the terrain at that index. The ...
- [LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- [LeetCode] Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- Leetcode: Pacific Atlantic Water Flow
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
随机推荐
- linux---nginx服务nfs服务nginx反向代理三台web
一:nginx服务 1.二进制安装nginx包 [root@bogon ~]# systemctl disable firewalld #关闭Firewalls自启动 Removed symlink ...
- python 黑客书籍 ——扫描+暴力破解
https://legacy.gitbook.com/book/germey/net-security/details 网络安全 介绍 构建一个端口扫描器 利用Pexpect模拟SSH连接 利用Pxs ...
- 007-对象—— static静态方法属性内存构成及使用方法讲解
<?php /* 7 对象 static静态方法属性内存构成及使用方法讲解 */ /*class a{ public $uname=11; static public function _a() ...
- 转载:【Oracle 集群】RAC知识图文详细教程(三)--RAC工作原理和相关组件
文章导航 集群概念介绍(一) ORACLE集群概念和原理(二) RAC 工作原理和相关组件(三) 缓存融合技术(四) RAC 特殊问题和实战经验(五) ORACLE 11 G版本2 RAC在LINUX ...
- 内存保护机制及绕过方法——通过伪造SEHOP链绕过SEHOP保护机制
1.1 SEHOP保护机制 1.1.1 SEHOP工作原理: SEHOP保护机制的核心就是检查SEH链的完整性,其验证代码如下: BOOL RtlIsValidHandler(handle ...
- canvas 遮罩
上一篇介绍了CSS3可以实现mask的方式,本篇介绍canvas同样也可以实现遮罩的方法: 原理: canvas是在画布上绘图,可以绘制各种形状,同时可以在一个层上重复画图,默认情况下后面的会覆盖前面 ...
- 【移动互联网开发】Zepto 使用中的一些注意点 【转】
前段时间完成了公司一个产品的 HTML5 触屏版,开发中使用了 Zepto 这个著名的 DOM 操作库. 为什么不是 jQuery 呢?因为 jQuery 的目标是兼容所有主流浏览器,这就意味着它的大 ...
- WiFi无线网络参数 802.11a/b/g/n 详解
转载自:WiFi无线网络参数 802.11a/b/g/n 详解 如转载侵犯您的版权,请联系:2378264731@qq.com 802.11a/b/g/n,其实指的是无线网络协议,细分为802.11a ...
- ETL学习整理 PostgreSQL
ETL分别是“Extract”.“ Transform” .“Load”三个单词的首字母缩写也就是“抽取”.“转换”.“装载”,但我们日常往往简称其为数据抽取. ETL是BI/DW(商务智能/数据仓库 ...
- Data truncated for column
数据类型不合法造成的.检查插入的数据.