Skip to the code
cf.
+10 XP

#724 Visible Rows with a Fixed Height

easy

A virtualized table has count rows, each exactly rowHeight pixels tall. Row i covers pixels from i * rowHeight up to (not including) (i + 1) * rowHeight. The view shows pixels from scrollTop up to (not including) scrollTop + viewportHeight.

Write fixedWindow(count, rowHeight, scrollTop, viewportHeight, overscan) that returns [start, end], the first and last row index to render. A row is visible if it shares at least one pixel with the view. Then extend the range by overscan rows on each side, without going below 0 or past count - 1. If no row is visible, return an empty array.

Example 1

Input: count = 1000, rowHeight = 40, scrollTop = 130, viewportHeight = 200, overscan = 2
Output: [1,10]

Rows 3 to 8 touch pixels 130 to 329; two extra rows on each side give [1, 10].

Example 2

Input: count = 5, rowHeight = 50, scrollTop = 0, viewportHeight = 1000, overscan = 1
Output: [0,4]

Everything fits, and the range can't go past the last row.

Constraints

  • 0 <= count <= 10^9
  • 1 <= rowHeight, viewportHeight <= 10^4
  • 0 <= scrollTop <= 10^12, 0 <= overscan <= 50
Saved in this browser

Run 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).