CS/Algorism

99클럽 코테 스터디 28일차 TIL: [LeetCode] 2145. Count the Hidden Sequence

olsohee 2024. 6. 17. 10:32

https://leetcode.com/problems/count-the-hidden-sequences/description/

class Solution {
    public int numberOfArrays(int[] differences, int lower, int upper) {
        long a = 0, ma = 0, mi = 0;
        for (int d: differences) {
            a += d;
            ma = Math.max(ma, a);
            mi = Math.min(mi, a);
        }
        return (int)Math.max(0, (upper - lower) - (ma - mi) + 1);
    }
}