Pour Water:

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.

注意事项:

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

样例:

 Example_1:
Given: heights = [2,1,1,2,1,2,2], V = 4, K = 3
Return: [2,2,2,3,2,2,2] Example_2:
Given: heights = [1,2,3,4], V = 2, K = 2
Return: [2,3,3,4] Example_3:
Given: heights = [3,1,3], V = 5, K = 1
Return: [4,4,4]

题目大意:给定一个组高度值,代表一个水槽的底部高度分布情况。在K点处倒入V体积的水,求倒水之后的高度分布。

 class Solution {
public int findLeftMinIdx(int[] heights, int K) {
int minIdx = K, minHeight = heights[K];
for (int i = K - 1; i >= 0; i--) {
if (heights[i] < minHeight) {
minIdx = i;
minHeight = heights[i];
}
else if (heights[i] > minHeight) {
break;
}
}
return minIdx;
} public int findRightMinIdx(int[] heights, int K) {
int minIdx = K, minHeight = heights[K];
for (int i = K + 1; i < heights.length; i++) {
if (heights[i] < minHeight) {
minIdx = i;
minHeight = heights[i];
}
else if (heights[i] > minHeight) {
break;
}
}
return minIdx;
} public int[] pourWater(int[] heights, int V, int K) {
for (int i = 0; i < V; i++) {
int leftMinIdx = findLeftMinIdx(heights, K);
if (leftMinIdx < K) {
heights[leftMinIdx]++;
}
else {
int rightMinIdx = findRightMinIdx(heights, K);
if (rightMinIdx > K) {
heights[rightMinIdx]++;
}
else {
heights[K]++;
}
}
}
return heights;
}
}

LintCode——Pour Water的更多相关文章

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

  2. [LeetCode] Pour Water 倒水

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

  3. LeetCode 755. Pour Water

    原题链接在这里:https://leetcode.com/problems/pour-water/description/ 题目: We are given an elevation map, hei ...

  4. ZOJ 3913 Bob wants to pour water

    ZOJ Monthly, October 2015 K题 二分答案+验证 #include<iostream> #include<algorithm> #include< ...

  5. zoj 2974 Just Pour the Water矩阵快速幂

    Just Pour the Water Time Limit: 2 Seconds      Memory Limit: 65536 KB Shirly is a very clever girl. ...

  6. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  7. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  8. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

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

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

随机推荐

  1. scp 利用 ssh 协议 复制文件

    有时候,我们使用 scp 命令可以解决我们很多问题: NAME scp — secure copy (remote file copy program) 使用举例:拷贝当前文件,到 系统 10.66. ...

  2. python基础学习2

    一.算数运算符 +加法,-减法,*乘法,/除法,//地板除,%求余,**幂运算. 二.逻辑运算符 非not.且and.或or.优先级依次为not,and,or. 三.print()end结尾 prin ...

  3. 转战JS(1) 初探与变量类型、运算符、常用函数与转换

    转战JS(1)初探与变量类型.运算符.常用函数与转换 做为一名.NET后台开发人员,正考滤向Web前端开发转型,之前也写过一代前端代码,可是当再回头看JS,并有转向它的意愿的时候,突然发现:原来JS不 ...

  4. Gitkraken的使用

    一个优秀的团队合作离不开git,一个优秀的程序员也离不开git.gitkraken是我在进行软工实践这门课接触到的git的UI界面的工具,它给我留下的印象就是非常好用和方便 怎么个方便法呢? 方便的安 ...

  5. csrf在web网站中有多重要

    小弟是学python的,今天在上网时看到一个商城网站,正好昨天学到了CSRF跨站请求,就对这个商城网站进行了一波测试 可以看到网页布局做的还是很不错的,然后进入了注册页面看看 之后就开始测试了 正常请 ...

  6. Mysql表创建外键报错

    数据库表A: CREATE TABLE task_desc_tab ( id INT(11) PRIMARY KEY NOT NULL COMMENT '自增主键' AUTO_INCREMENT, t ...

  7. google浏览器window.onbeforeunload方法兼容问题

    window.onbeforeunload方法在IE内核浏览器是有效的,但是在google浏览器中并不兼容,请教给位怎么在google浏览器中兼容window.onbeforeunload方法 采纳的 ...

  8. 浏览器中上传Excel文件,服务器获取Excel字段。写入的数据库中。操作Excel的方式jxl和poi。

    从Excel中获取字段,官方给我们提供了方法,地址https://poi.apache.org/components/spreadsheet/quick-guide.html#CellContents ...

  9. 1、JUC--volatile 关键字-内存可见性

    Java JUC简介 在 Java 5.0 提供了 java.util.concurrent (简称JUC )包,在此包中增加了在并发编程中很常用的实用工具类,用于定义类似于线程的自定义子系统,包括线 ...

  10. [luogu2469] 星际竞速

    题面 ​ 巨佬一眼就能看出这是最小路径覆盖, 我这个蒟蒻还是太弱了... ​ 我们可以知道跳跃值为点权w[i], 两点之间距离为边权ww ​ 对于每个点, 在最小路径覆盖问题中, 假设每个点都是一条路 ...