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. PHP 传值操作和传地址操作

    PHP中有两种赋值操作 一种是传值操作 example: $a = 1; $b = $b; 这个就是传值 传地址是: example2: $a = 1; $b = &$a; 这两者有什么区别呢 ...

  2. ES6+转ES5

    npm init //创建package.json文件 下载转换babel库及其100+依赖 npm install babel-cli -D npm install babel-preset-env ...

  3. gnome美化

    调整工具更新可以移动窗口控件gnome-tweak-tool # dnf install gnome-tweak-tool 命令行启动,并且要在普通用户下启动 $ gnome-tweak-tool 在 ...

  4. load data infile出现“ERROR 13 (HY000): Can't get stat of '/tmp/test2.txt' (Errcode: 2)”问题

    用load data infile导数据到mysql数据库出现这个该问题,解决方法如下: 安全起见,连接mysql的语句需要添加–local-infile, mysql -hlocalhost -ur ...

  5. leetcode 486 预测赢家

    题目描述 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端拿取分数,然后玩家1拿,--.每次一个玩家只能拿取一个分数,分数被拿取之后不再可取.直到没 ...

  6. Alpha冲刺报告(3/12)(麻瓜制造者)

    今日已完成情况: 肖小强: 解决了之前的部署demo问题,学习了基本需要的api 江郑: 进行发布需求数据库的增删改查 邓弘立: 完成了轮播图的设计 刘双玉: 编码发布商品的接口,数据库表的修改 汪志 ...

  7. 【Android自动化】unittest测试框架关于用例执行的几种方法

    # -*- coding:utf-8 -*- import unittest class test(unittest.TestCase): def setUp(self): print 'This i ...

  8. ethjs-1-了解

    https://github.com/ethjs/ethjs/blob/master/docs/user-guide.md Install npm install --save ethjs Usage ...

  9. webservice 客户端调用

    /** * 通过webserevice下发工单 * @param url * @param method * @param requestMap * @return * @throws Service ...

  10. nrf52832 连接参数更新过程

    ble 连接参数更新过程如下: 一般分三个过程 一 .主机发起连接(带有一个连接参数,一般都是 7.5ms) 二.主机更新连接参数 (举例:NRF CONNECT 安卓app软件 45ms). 三.从 ...