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. [BZOJ 3167][HEOI 2013]SAO

    [BZOJ 3167][HEOI 2013]SAO 题意 对一个长度为 \(n\) 的排列作出 \(n-1\) 种限制, 每种限制形如 "\(x\) 在 \(y\) 之前" 或 & ...

  2. 二、git版本回退

    查看历史版本提交记录 git log git log --pretty=oneline Git用 HEAD表示当前版本 commit id(版本号),也就是最新的提交e4aa53d...43ae6f6 ...

  3. div设置contenteditable="true" 光标消失:原因

    原因1:document.onselectstart= function(){return false;}; 原因2:父层设置了user-select:none 导致 子层设置了 contentedi ...

  4. HTML5中的Canvas详解

    什么是Canvas HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成.<canvas> 标签只是图形容器,您必须使用脚本来绘制图 ...

  5. Swift Package Manager(一)初探

    一句话:Swift Package Manager(swift包管理器,简称:SPM)就是在swift开发中用来替代CocoaPod的:在swift开发中,SPM完全可以替代CocoaPod的功能,并 ...

  6. C#创建无窗体的应用程序

    示例程序 这是初学C#时困惑了很久才解决的问题,突然想起来拿出来和大家分享. 当初我是这样做的: 1.      在窗体初始化时(构造函数里面),添加一句This.Visible = false; 2 ...

  7. kubernetes备份和恢复

    kubernetes备份和恢复   备份etcd数据 首先由于ETCD有三个备份,并且会同步,所以您只需要在一台master机器上执行ETCD备份即可. 另外在运行下列命令前,确保当前机器的kube- ...

  8. 【Java123】JavaWeb Servlet开发

    http://www.runoob.com/servlet/servlet-intro.html https://www.cnblogs.com/xdp-gacl/tag/JavaWeb学习总结/de ...

  9. day3-课堂代码

    # a = ('哈哈', 'xixi', 'hehe') # print(a[0]) # print(a[0:2]) # # # 列表 # a = ['哈哈', 'xixi', 'hehe', 1, ...

  10. MP实战系列(十七)之乐观锁插件

    声明,目前只是仅仅针对3.0以下版本,2.0以上版本. 意图: 当要更新一条记录的时候,希望这条记录没有被别人更新 乐观锁实现方式: 取出记录时,获取当前version 更新时,带上这个version ...