A feed renders rows with estimated heights, then measures the real heights after they appear. If rows above the reader change height, the content would jump. To prevent that, the list keeps an anchor: the first row that is at least partly visible stays at the same distance from the top of the view.
Write anchoredScrollTop(heights, scrollTop, updates). heights are the current heights, stacked from pixel 0. The anchor is the first row whose bottom is greater than scrollTop; remember offset = scrollTop - anchorTop. Then apply every [index, newHeight] in updates and return newAnchorTop + offset. If no row ends below scrollTop, return scrollTop unchanged.
Input: heights = [50,50,50,50], scrollTop = 120, updates = [[0,80],[1,60]] Output: 160
The anchor is row 2 (pixels 100 to 149), 20 px into it. Rows 0 and 1 grow by 40 px in total, so row 2 now starts at 140 and the answer is 140 + 20.
Input: heights = [50,50,50,50], scrollTop = 120, updates = [[3,500]] Output: 120
Only a row below the anchor changed, so nothing above the reader moved.
0 <= heights.length <= 10^5, 1 <= heights[i] <= 10^40 <= updates.length <= 10^5, each with a valid index and 1 <= newHeight <= 10^40 <= scrollTop <= 10^9Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #727 Keep the Reading Position After Measuring. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.