Feed scroll anchor correction
Your task
A virtualised feed re-measures its rows once images finish loading, and the scroll offset has to be corrected so the reader is not thrown. old_heights and new_heights give the pixel height of every row before and after the re-measure, in the same order and always the same length, and a height may be 0. Content begins at pixel 0 and each row sits directly below the one before it. The anchor is the row whose band holds scroll_top, where a row covers its top pixel up to but not including its bottom, so a zero height row can never be the anchor. Keep the reader the same distance into the anchor row, and when that row is now shorter than the distance, use its new height instead. Clamp the answer to the range 0 up to the new total height minus viewport, and return that maximum when scroll_top sat at or past the old content. Heights, scroll_top and viewport are never negative.
Examples
anchored_scroll_top(old_heights: list[int], new_heights: list[int], scroll_top: int, viewport: int) -> intanchored_scroll_top([100, 100, 100, 100, 100, 100], [150, 100, 100, 100, 100, 100], 250, 100)300anchored_scroll_top([100, 200, 100, 100, 100], [100, 20, 100, 100, 100], 250, 100)120anchored_scroll_top([30, 30, 30], [30, 30, 30], 45, 30)45