Opening your workspace…
Workspace/Coding room
Opening your workspace…
The tasting judge dips the same silver spoon into each pour and frowns whenever it gets too warm or too cold. Pour i has temperature temperatures[i]. She will award a ribbon to every run of consecutive pours with a length from minPours through maxPours, inclusive, whose hottest and coolest pours differ by at most tolerance. Return the number of ribbons she needs. Runs with different start or end positions each get a ribbon, even when the thermometer readings match.
[3,4,6,5], 2, 2, 3Output4All three runs of length 2 are steady. Of the two runs of length 3, only [4, 6, 5] stays within a range of 2, which makes four in total.
[8,8,8], 0, 1, 3Output6Every run has a range of zero: three of length 1, two of length 2 and one of length 3.
Use two monotonic queues of indices to track the minimum and maximum of a moving window. For each right end, the starts that keep the temperature range within tolerance form one interval, and the starts allowed by the length limits form another. Count the positions that are in both.
Learn why nonnegative totals let a left boundary move only forward.
Try this problem →Class S · Arrays / Hash maps / Sliding windowReuse the interval-of-starts counting idea with a different window validity rule.
Try this problem →/** @param {number[]} temperatures * @param {number} tolerance * @param {number} minPours * @param {number} maxPours * @returns {number} */ function solve(temperatures, tolerance, minPours, maxPours) { // Count runs satisfying both temperature and length limits. }
Tab inserts two spaces. Shift+Tab outdents. Use the arrow keys and Tab to choose a suggestion. Press Escape, then Tab, to leave the editor. Press Control or Command with apostrophe to run samples or with Enter to submit.
[3,4,6,5], 2, 2, 34