Trains the technique from
LeetCode 327Count of Range SumThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A reservoir logs one net level change per hour in nums, where a negative entry means the level dropped that hour and a positive entry means it rose.
A stretch is any run of one or more consecutive hours, written nums[i .. j] with i <= j. Its swing is the sum of the entries it covers. Two stretches are different when their hour ranges differ, even if their swings agree.
Given the inclusive band lower to upper, return how many stretches have a swing of at least lower and at most upper.
Example 1
Eight stretches land inside the band: `nums[0..0]`, `nums[0..1]`, `nums[0..2]`, `nums[0..4]`, `nums[1..3]`, `nums[2..2]`, `nums[2..4]` and `nums[3..4]`, with swings of 3, -1, 1, 0, 3, 2, 1 and -1. Every one of those swings sits between -1 and 4 inclusive.
Example 2
Only `nums[0..1]` sums to 0. The single-hour stretches swing by 8 and -8, both outside the band.
Example 3
The one stretch available swings by 6, and 6 lies inside the band because both ends of the band count.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def count_range_sum(nums: list[int], lower: int, upper: int) -> int:public int countRangeSum(int[] nums, int lower, int upper)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.