原题链接在这里: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:

  1. heights will have length in [1, 100] and contain integers in [0, 99].
  2. V will be in range [0, 2000].
  3. 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的更多相关文章

  1. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  2. 【LeetCode】365. Water and Jug Problem 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学题 相似题目 参考资料 日期 题目地址:http ...

  3. 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 ...

  4. 【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 ...

  5. LintCode——Pour Water

    Pour Water: We are given an elevation map, heights[i] representing the height of the terrain at that ...

  6. [LeetCode] Pour Water 倒水

    We are given an elevation map, heights[i] representing the height of the terrain at that index. The ...

  7. [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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. mysql数据库开放远程连接的方法

    web与mysql数据库分离开来是一个不错的选择,避免因为大量的数据库查询占用CPU而使web资源不足,同时可以使web服务器的资源尽最大的提供浏览服务,而数据库服务器单独的只处理数据库事物. 适用范 ...

  2. ASP.NET 4.5 MVC 4 无法运行在Windows2008的IIS7.0上显示404的解决方案

    需要在web.config下加上这个 <system.webServer> <modules runAllManagedModulesForAllRequests="tru ...

  3. 设计模式--组合模式C++实现

    组合模式C++实现 1定义 将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性 2类图 角色分析 Component抽象构建角色 定义参加组合独享的共同方 ...

  4. nyoj——5字符串STL复习

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  5. HDU 4696 Answers (脑补+数形结合)

    题意 给一个图,每个点的出度为1,每个点的权值为1或者2.给Q个询问,问是否能找到一条路径的权值和M. 思路 由于每个点的出度为1,所以必然存在环.又因为c[i]只能取1或者2,可以组成任意值,所以只 ...

  6. 最近玩了一下qt5.2.1,顺着写点东西,关于这个版本设置程序主窗口居中

    #include <QtGui/QGuiApplication> #include <QDebug> #include <QScreen> #include &qu ...

  7. 024——VUE中filter的使用

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. This function has none of DETERMINISTIC, NO SQL解决办法

    This function has none of DETERMINISTIC, NO SQL解决办法 创建存储过程时 出错信息: ERROR 1418 (HY000): This function ...

  9. rsync的服务启动脚本

    #!/bin/bash #author:Mr.chen # chkconfig: # description:This is Rsync service management shell script ...

  10. c# JScriptProvider包装

    using System; using System.CodeDom.Compiler; using System.Reflection; using System.Web.UI; using Mic ...