leetcode 2145.count the hidden sequences

wonderful world·2022년 1월 23일
0

leetcode

목록 보기
19/21

https://leetcode.com/contest/biweekly-contest-70/problems/count-the-hidden-sequences/
biweekly contest. problem #2

class Solution:
    def numberOfArrays(self, diffs: List[int], lower: int, upper: int) -> int:
        def f(diffs, lower, upper):
            # calculate actual range and then take given range. 
            # the difference is the count
            acc_change = []
            last = 0
            for d in [0]+diffs:
                v = last + d
                acc_change += [v]
                last = v
            lowest = min(acc_change)
            highest = max(acc_change)
            actual_range = highest - lowest
            given_range = upper - lower
            count = given_range - actual_range + 1
            return max(count, 0)
        
        return f(diffs, lower, upper)
profile
hello wirld

0개의 댓글