2019独角兽企业重金招聘Python工程师标准>>>

问题:

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

    Example 1:

    Input: heights = [2,1,1,2,1,2,2], V = 4, K = 3
    Output: [2,2,2,3,2,2,2]
    Explanation:
    #       #
    #       #
    ##  # ###
    #########0123456    <- index
    The first drop of water lands at index K = 3:
    #       #
    #   w   #
    ##  # ###
    #########0123456
    When moving left or right, the water can only move to the same level or a lower level.
    (By level, we mean the total height of the terrain plus any water in that column.)
    Since moving left will eventually make it fall, it moves left.
    (A droplet "made to fall" means go to a lower height than it was at previously.)
    #       #
    #       #
    ## w# ###
    #########0123456
    Since moving left will not make it fall, it stays in place.  The next droplet falls:
    #       #
    #   w   #
    ## w# ###
    #########0123456
    Since the new droplet moving left will eventually make it fall, it moves left.
    Notice that the droplet still preferred to move left,
    even though it could move right (and moving right makes it fall quicker.)
    #       #
    #  w    #
    ## w# ###
    #########0123456
    #       #
    #       #
    ##ww# ###
    #########0123456
    After those steps, the third droplet falls.
    Since moving left would not eventually make it fall, it tries to move right.
    Since moving right would eventually make it fall, it moves right.
    #       #
    #   w   #
    ##ww# ###
    #########0123456
    #       #
    #       #
    ##ww#w###
    #########0123456
    Finally, the fourth droplet falls.
    Since moving left would not eventually make it fall, it tries to move right.
    Since moving right would not eventually make it fall, it stays in place:
    #       #
    #   w   #
    ##ww#w###
    #########0123456
    The final answer is [2,2,2,3,2,2,2]:#    ####### ####### 0123456

    Example 2:

    Input: heights = [1,2,3,4], V = 2, K = 2
    Output: [2,3,3,4]
    Explanation:
    The last droplet settles at index 1, since moving further left would not cause it to eventually fall to a lower height.

    Example 3:

    Input: heights = [3,1,3], V = 5, K = 1
    Output: [4,4,4]

    Note:

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

解决:

【题意】

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

倒入的每一体积的水按照如下规则进行流动:

  1. 如果在K点左侧存在地势较低且可达的位置,则水优先向左流动。
  2. 否则,在K点右侧存在地势较低且可达的位置,则水向右侧流动。
  3. 否则,水停留在K处。

①  直接模拟上面的流程。

class Solution { //7ms
    public int[] pourWater(int[] heights, int V, int K) {
        for (int i = 0;i < V;i ++){
            int leftMinIndex = findLeftMinIndex(heights,K);
            if (leftMinIndex < K){
                heights[leftMinIndex] ++;
            }else {
                int rightMinIndex = findRightMinIndex(heights,K);
                if (rightMinIndex > K){
                    heights[rightMinIndex] ++;
                }else {
                    heights[K] ++;
                }
            }
        }
        return heights;
    }
    public int findLeftMinIndex(int[] heights,int K){
        int minIndex = K;
        int minHeight = heights[K];
        for (int i = K - 1;i >= 0;i --){
            if (heights[i] < minHeight){
                minIndex = i;
                minHeight = heights[i];
            }else if (heights[i] > minHeight){
                break;
            }
        }
        return minIndex;
    }
    public int findRightMinIndex(int[] heights,int K){
        int minIndex = K;
        int minHeight = heights[K];
        for (int i = K + 1;i < heights.length;i ++){
            if (heights[i] < minHeight){
                minIndex = i;
                minHeight = heights[i];
            }else if (heights[i] > minHeight){
                break;
            }
        }
        return minIndex;
    }
}

转载于:https://my.oschina.net/liyurong/blog/1616708

水槽中倒水,Pour Water相关推荐

  1. 【Week2 作业】A - Maze、B - Pour Water

    A - Maze 题意: 东东有一张地图,想通过地图找到妹纸.地图显示,0表示可以走,1表示不可以走,左上角是入口,右下角是妹纸,这两个位置保证为0.既然已经知道了地图,那么东东找到妹纸就不难了,请你 ...

  2. ZOJ2974 Just Pour the Water(5th 浙江省赛)矩阵快速幂

    问题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2974 问题描述: Just Pour the Water Tim ...

  3. ZOJ 1973 Just Pour the Water(矩阵快速幂)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1973 Shirly is a very clever girl. N ...

  4. LeetCode | 0365. Water and Jug Problem水壶问题【Python】

    LeetCode 0365. Water and Jug Problem水壶问题[Medium][Python][BFS][数学] Problem LeetCode You are given two ...

  5. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  6. LeetCode-3.21-365-M-水壶问题(Water and Jug Problem)

    文章目录 思路-wait 解法 有两个容量分别为 x升 和 y升 的水壶以及无限多的水.请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水? 如果可以,最后请用以上水壶中的一或两个来盛放取得的 ...

  7. Google发布TCC 更好的理解视频中事件逻辑

    通过TCC模型,可以有效的理解视频中可能触发的事件. 原文 http://ai.googleblog.com/2019/08/video-understanding-using-temporal.ht ...

  8. 【操作系统】某寺庙,住着一个老和尚和若干小和尚,有一个水缸,由小和尚提水入缸供老和尚饮用。水缸可以容纳10桶水,水取自同一口井中,由于水井口窄,每次只能容纳一个水桶取水,水桶总数为3个。每次往水缸中倒

    题目 某寺庙,住着一个老和尚和若干小和尚,有一个水缸,由小和尚提水入缸供老和尚饮用.水缸可以容纳10桶水,水取自同一口井中,由于水井口窄,每次只能容纳一个水桶取水,水桶总数为3个.每次往水缸中倒水与从 ...

  9. UE4 插件Water系统

    一.Water插件介绍 UE4的水体系统,能够让我们用样条线定义海洋,湖泊,江河以及岛屿等.让我们可以调节和显现河流各段的深度,宽度和流流速,以及海洋与湖泊上波浪的波长,振幅,方向和坡度.内置的流体模 ...

  10. 《炬丰科技-半导体工艺》处理液中溶解氧对硅晶片表面的影响

    书籍:<炬丰科技-半导体工艺> 文章:处理液中溶解氧对硅晶片表面的影响 编号:JFHL-21-1062 作者:炬丰科技 引言 在当今最先进的科学和技术领域中,有许多需要以原子级的顺序控制形 ...

最新文章

  1. html标签自动对齐,sublime,jsx里的html标签自动缩进对齐的插件或者配置?
  2. 广文艺计算机综合美术,广东文艺职业学院2018年第二批合同制人员招聘专业技能考核和试讲题目...
  3. Spring容器与上下文理解
  4. logistic回归 如何_R_语言 logistic回归分析
  5. SQLplus 和mysql区别_mysql和oracle的区别有哪些
  6. resent代码详解
  7. rgba与16进制颜色格式互转
  8. Ubuntu系统未发现vim命令
  9. 微信域名防封的新知识
  10. cfg文件怎么改回计算机程序,我把一个拓展名为cfg文件用word打开后,凡是cfg的文件图标都变成了word,肿么改回去?...
  11. 分智评25位最受欢迎CEO 微软中国梁念坚居首
  12. C++实现控制台迷宫小游戏
  13. 安装新操作系统需要注意的问题
  14. 无刷新假象   实现简易文件上传
  15. Java 与 区块链技术_java区块链技术有哪些主要的特点和应用
  16. 修复WIN10下Prolific USB-to-Serial Comm Port驱动无法使用
  17. Node.js 运行.js文件出现错误找不到文件的解决办法
  18. 运营商常见的大数据业务学习笔记
  19. java编程官方教程_Java编程入门官方教程
  20. 自定义鼠标指针——让你的指针瞬间变美

热门文章

  1. 安全提示:IIS不要开启“WebDAV”扩展
  2. PHPMailer 报错:SMTP ERROR: Password command failed: 535 Login Fail
  3. PHP静态方法中调用非静态方法
  4. Matlab系列教程_基础知识_基本矩阵操作
  5. 要参与OpenJDK8源码修改,从哪里下源码?
  6. checking for spandsp >= 3.0... configure: error: no usable spandsp; please install spandsp3 devel pa
  7. MAC使用find命令的正确办法
  8. 奇怪的规律:飞机事故总是凑在一段时间内
  9. 干电池很不经用,比充电电池差多了
  10. 现在一行代码允许长度,80太少,120才算正常