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. 斯诺克台球比赛规则 (Snooker)

    斯诺克台球比赛规则 斯诺克(Snooker)的意思是“阻碍.障碍”,所以斯诺克台球有时也被称为障碍台球.此项运动使用的球桌长约3569毫米.宽1778毫米,台面四角以及两长边中心位置各有一个球洞,使用 ...

  2. python第三十六课——2.迭代器对象

    满足前提: 1).必须是一个可迭代对象 2).可以被next()所作用的 举例: generator... 高效的检测一个对象是否是迭代器对象 需要使用collections模块中的Iterator类 ...

  3. 敲代码非常难之logstash之file input插件实现分析

    版权声明:本文为横云断岭原创文章,未经博主同意不得转载.微信公众号:横云断岭的专栏 https://blog.csdn.net/hengyunabc/article/details/25665877 ...

  4. php输出日志

    error_log('你要输出的信息', 3, 'E:\work\jiajiayue\Application\Api\Controller\1.txt');die; php error_log记录日志 ...

  5. Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换(转载)

    第二篇:JAVA字符编码系列二:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换 1.函数介绍在Java中,字符串用统一的Unicode编码,每个字符占用两个字节,与编码有关的两 ...

  6. AxisWebservice 发送多参数配置

    1.在web.xml中配置代码如下 <servlet> <servlet-name>AxisServlet</servlet-name> <display-n ...

  7. JAVA框架 Spring 事务

    一.我们之前在hibernate的时候,需要直接写事务,需要绑定当前线程保证获取同一个连接,虽然hibernate的帮我们封装绑定当前现成的操作,但是需要我们手动的去开启和关闭事务. 而spring帮 ...

  8. day45

    今日内容 1.css三种引入方式 2.三种引入方式的优先级 3.长度及颜色单位 4.常用样式 5.css选择器 CSS三种引入方式 1.1css引入方式之行间式 ​ 行间式(特点): ​ 1.标签头部 ...

  9. jqgrid 在表格底部添加自定义按钮

    往往我们需要在jqgrid底部的分页行中添加一些自定义按钮,效果如下: 上图中,三个按钮均是自定义添加上的. 1.要新增自定义按钮在表格底部,仍离不开分页div,需要给jqgrid绑定分页方法 2.由 ...

  10. 基于树莓派3的CAN总线编程

    基于树莓派3的CAN总线编程 原创 2016年09月08日 10:16:13 标签: 树莓派3 / MCP2515 / CAN总线 / python / 命令行 5254 简介 树莓派3使用Pytho ...