Opening your workspace…
Workspace/Coding room
Opening your workspace…
Under the ledger lives a silverfish with a taste for arithmetic. She may nibble out one strip of consecutive entries, at least one entry long, provided its sum comes to exactly target. The entries in adjustments are token adjustments, so debts and zeros are on the menu. How many strips could she choose? Return the count. Strips with different starting or ending positions are separate choices, even if they taste exactly alike.
[3,-1,-2,3], 3Output3Three strips add up to 3: index 0 on its own, indices 0 through 3, and index 3 on its own.
[0,0,0], 0Output6Every strip adds up to zero. There are three of length 1, two of length 2 and one of length 3.
The total of a strip is the difference between two running totals. For each position where a strip could end, how many earlier running totals would give a difference of target?
Compare looking up a complement with looking up a difference of prefix totals.
Try this problem →Class S · Arrays / Hash maps / Sliding windowCount contiguous ranges under distinct-value and length constraints instead of a sum constraint.
Try this problem →/** @param {number[]} adjustments * @param {number} target * @returns {number} */ function solve(adjustments, target) { // Count nonempty contiguous strips whose total is target. }
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,-1,-2,3], 33