美文网首页
[Lint Code] Pour Water

[Lint Code] Pour Water

作者: Harvest | 来源:发表于2018-05-07 10:56 被阅读44次

    原题描述

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

    嗯,完全的 ,看不懂嘛,找波翻译加参考:
    大意:

    给定一个组高度值,代表一个水槽的底部高度分布情况。在K点处倒入V体积的水,求倒水之后的高度分布。
    倒入的每一体积的水按照如下规则进行流动
    如果在K点左侧存在地势较低且可达的位置,则水优先向左流动。
    否则,在K点右侧存在地势较低且可达的位置,则水向右侧流动。
    否则,水停留在K处

    这题关键在于存在地势较低且可达的位置,什么意思?
    我理解为,
    [3,2,2,1,3]
    从索引1开始的话,那么第一次分配的就是索引为3值为1的位置,
    这样就变为[3,2,2,2,3],
    如果继续分配,那么就不存在地势低且可达,就只有这样
    [3,3,2,2,3]
    再继续分
    [3,3,3,2,3]..
    附上lintcode ra代码 python3版:

    import time
    from functools import wraps
    # 运行时间
    def countTime(func):
        @wraps(func)
        def wrap(*args,**kwargs):
            start = time.time()
            func(*args,**kwargs)
            print(time.time() - start)
        return wrap
    class Solution:
        """
        @param heights: the height of the terrain
        @param V: the units of water
        @param K: the index
        @return: how much water is at each index
        """
        @countTime
        def pourWater(self, heights, V, K):
            # Write your code here
            def leftFoundMin(l, c):
                target = c
                while True:
                    if heights[l] < heights[c]:
                        c = l
                        l -= 1
                        target = c
                        if l < 0:
                            break
                        continue
                    elif heights[l] == heights[c]:
                        c = l
                        l -= 1
                        if l < 0:
                            break
                        continue
                    else:
                        break
                return target
    
            def rightFoundMin(r, c):
                """
                :param r: 
                :param c:
                :return:
                """
                target = c
                while True:
                    if heights[r] < heights[c]:
                        c = r
                        r += 1
                        target = c
                        if r >= len(heights):
                            break
                        continue
                    elif heights[r] == heights[c]:
                        c = r
                        r += 1
                        if r >= len(heights):
                            break
                        continue
                    else:
                        break
                return target
    
            while V > 0:
                if K - 1 >= 0:
                    l = leftFoundMin(K-1,K)
                else:
                    l=K
                if K + 1 < len(heights):
                    r = rightFoundMin(K+1,K)
                else:
                    r=K
                if l <K:
                    heights[l] += 1
                elif r >K:
                    heights[r] += 1
                elif l==r:
                    heights[K] += 1
                else:
                    pass
                V -= 1
            return heights
    

    相关文章

      网友评论

          本文标题:[Lint Code] Pour Water

          本文链接:https://www.haomeiwen.com/subject/zjjmrftx.html