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. R语言与数据分析(1) 数据分析流程、数据挖掘、数据可视化
  2. R语言爬取imdb电影海报
  3. 2011年使用率增长最快的十大Web技术
  4. 2021.4.23最新mac11.1 big sur 关于CocoaPods安装和使用
  5. springboot中的拦截器interceptor和过滤器filter,多次获取request参数
  6. SDN精华问答 | 了解SDN架构
  7. Building Roads(POJ-3625)
  8. 怎么把外部参照合并到图纸_怎么对两个图纸内容进行合并操作
  9. 完善征信+垂直创新 助力有融网“剩者为王”
  10. PHP 发送谷歌邮箱
  11. dvr行业的linux
  12. 苹果电脑双系统虚拟机怎么安装?
  13. python课程作业-贪吃蛇
  14. in field list is ambiguous 解决方法
  15. python局域网收集数据_Python实现扫描局域网活动ip
  16. win10计算机控制面板在哪里,教您win10控制面板在哪
  17. IDEA背景色和背景图片的设置
  18. 2000坐标系xy坐标几位_详解| 带你认识新一代坐标系——2000国家大地坐标系
  19. CNN 卷积神经网络-- 残差计算
  20. IP签名档PHP源码,简单几步,教你制作自己的显IP签名档

热门文章

  1. Pandas简单写入数据到csv文件
  2. 关于EasyUI中DataGrid控件的一些使用方法总结
  3. MAC中让右键菜单出现终端(命令行)打开文件夹的功能
  4. No package ‘dconf‘ found
  5. 路径中,连续多个目录分隔符不影响,仍按照一个处理
  6. 我设计的目录结构如此清楚,你为什么也会错
  7. 没有用括号确定操作符的优先级顺序,导致错误一例
  8. Linux下用C获取so库所在路径
  9. 全网首发:怎样加快android doubango的编译速度?
  10. Linux使用cups进行打印