LintCode——Pour Water
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的更多相关文章
- 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] Pour Water 倒水
We are given an elevation map, heights[i] representing the height of the terrain at that index. The ...
- LeetCode 755. Pour Water
原题链接在这里:https://leetcode.com/problems/pour-water/description/ 题目: We are given an elevation map, hei ...
- ZOJ 3913 Bob wants to pour water
ZOJ Monthly, October 2015 K题 二分答案+验证 #include<iostream> #include<algorithm> #include< ...
- zoj 2974 Just Pour the Water矩阵快速幂
Just Pour the Water Time Limit: 2 Seconds Memory Limit: 65536 KB Shirly is a very clever girl. ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- [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 ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
随机推荐
- soapui 调用wsdl 步骤以及出现的问题
创建soap project 添加wsdl 地址(或者导入wsdl(后缀可以是xml或者wsdl)文件) 添加request 自动生成请求代码 参数放在in0 中 <soapenv:Envelo ...
- Go语言学习笔记(四)结构体struct & 接口Interface & 反射reflect
加 Golang学习 QQ群共同学习进步成家立业工作 ^-^ 群号:96933959 结构体struct struct 用来自定义复杂数据结构,可以包含多个字段(属性),可以嵌套: go中的struc ...
- Grafana3.0.1+Zabbix3.0.4监控系统平台搭建
前言 本文的Zabbix部分知识只介绍它的基础安装,Zabbix的使用以及配置优化并不在本文的介绍范围之内. 本文只介绍在CentOS6系列下的安装和部署,其他发行版与其他版本号暂不涉及 本文默认使用 ...
- php header函数导出excel表格
推荐一个除了用PHPExcel导出表格之外的另外一种比较简单不需要引入类文件的表格导入方法——header()导出excel表格. 导出表格的步骤封装成了方法,以便于重复使用,代码如下: /** * ...
- python之生成随机测验试卷
自己又开始懒散的态度生活,所以几乎有两个月没有更博了. 项目:美国各州首府地理考试,为防止作弊,35份试卷,50道题随机次序,生成独一无二的试卷. 基本想法: 1.将各州首府的地方和首府写入列表,以K ...
- Win10环境Tensorflow-GPU13.1/JupyterNotebook的安装
参考 : Anaconda Tensorflow GPU 版本的安装问题 https://blog.csdn.net/u010977034/article/details/62038698 Windo ...
- python 通过shutil实现文件后缀名判断及复制
In [18]: for file in os.listdir('.'): ...: if os.path.splitext(file)[1] == '.html': ...: print(file) ...
- 非阻塞 sleep
在OpenResty里面选择使用库的时候,有一个基本的原则:尽量使用ngx Lua的库函数,尽量不用Lua的库函数,因为Lua的库都是同步阻塞的. 再来一个例子来说明阻塞API的调用对nginx并发性 ...
- WorldWind源码剖析系列:经纬度格网类LatLongGrid
经纬度格网类LatLongGrid继承自可渲染对象类RenderableObject,是WorldWind中用来在星球外表绘制经纬度格网的封装类.其类图如下所示. 绘制经纬网格的主体函数为Render ...
- 【转】SQL 常用关键字释义和用法
转自: http://blog.csdn.net/iamwangch/article/details/8093933 下面 是 从网络上整理 出来的 SQL 关键字和 常用函数的 释义和简单用 ...