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 Vunits 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:
heightswill have length in[1, 100]and contain integers in[0, 99].Vwill be in range[0, 2000].Kwill 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 ...
随机推荐
- #if 0 #endif && #if 1 #endif ----待整理
在看一个 usbcan 的上位机例程中发现了这个,于是百度下,记录下来.(参考:http://nevel.cnblogs.com/p/6378035.html)
- C#转译字符
C#转义字符: 一种特殊的字符常量 以反斜线"\"开头,后跟一个或几个字符 具有特定的含义,不同于字符原有的意义,故称“转义”字符. 主要用来表示那些用一般字符不便于表示的控制代码 ...
- bzoj1001平面图最小割转对偶图最短路
https://www.lydsy.com/JudgeOnline/problem.php?id=1001 很明显的求对偶图的最短路即可(由于特判写错了一直wa = = ) //#pragma com ...
- UVALive-5713 Qin Shi Huang's National Road System (次小生成树)
题目大意:有n个城市,要修一些路使得任意两个城市都能连通.但是有人答应可以不计成本的帮你修一条路,为了使成本最低,你要慎重选择修哪一条路.假设其余的道路长度为B,那条别人帮忙修的道路两端城市中的总人口 ...
- [转载]java获取word里面的文本
需求场景 开发的web办公系统如果需要处理大量的Word文档(比如有成千上万个文档),用户一定提出查找包含某些关键字的文档的需求,这就要求能够读取 word 中的文字内容,而忽略其中的文字样式.表格. ...
- C# 中移动文件到指定位置
根据文件后缀名称将文件移动到指定的文件夹下面,具体代码如下: demo中使用的是 .png 具体的情况根据你的需求可以更改 using System; using System.IO; public ...
- 【WebGL】4.光源
光的类型:所有的光都是从THREE.Light继承,分为环境光THREE.AmbientLight,点光源PointLight,聚光灯THREE.SpotLight和方向光THREE.Directio ...
- 《Effective C++》第2章 构造/析构/赋值运算(1)-读书笔记
章节回顾: <Effective C++>第1章 让自己习惯C++-读书笔记 <Effective C++>第2章 构造/析构/赋值运算(1)-读书笔记 <Effecti ...
- ios入门第一天
写在两个@ 之间的为oc语言 之外的为c语言 访问权限一旦定义了一个 除非在重新定义一个 否则都是该类型的 如 @protected int i; int j; int l;int n; ...
- Ubuntu12.04 中文输入法设置
1.ibus输入法 Ubuntu系统安装后已经自带了ibus输入法,在英语环境下默认不启动. 配置ibus自动启动可 以在ubuntu系统菜单上选择System(系统)--- Preferences( ...