Music streaming app~12 min2s · 256 MBtarget O(q) time, O(1) spacetraversal
The ticket page of a live-show app draws a hall of rows rows with columns seats in each row. Seats are numbered from 0 in one list: the front row from left to right, the next row from right to left, and so on, like a snake.
The page gets a list of seat numbers and must highlight each one. Rows and columns are counted from 0, with column 0 on the left.
Write seat_positions(rows, columns, seats).
seats[i] is a seat number, from 0 to rows × columns - 1.Input: rows = 4, columns = 5, seats = [0, 7, 14, 19]
Output: [0, 0, 1, 2, 2, 4, 3, 0]Divide the seat number by the row width for the row. The remainder is the place in the numbering. Odd rows count from the right.
| Seat | Row (seat ÷ 5) | Place (seat mod 5) | Column |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 7 | 1 | 2 | 2 (odd row: 4 - 2) |
| 14 | 2 | 4 | 4 |
| 19 | 3 | 4 | 0 (odd row: 4 - 4) |
Input: rows = 1, columns = 1, seats = [0]
Output: [0, 0]A hall of one seat has one position.
Input: rows = 2, columns = 3, seats = [3, 5]
Output: [1, 2, 1, 0]Seats 3 to 5 are in row 1, which is numbered from the right: seat 3 is column 2, and seat 5 is column 0.
1 ≤ rows ≤ 1,000 and 1 ≤ columns ≤ 1,0001 ≤ seats.length ≤ 100,000 (seats to highlight)0 ≤ seats[i] ≤ 999,999, and every seat number is below rows × columnsA hall can have up to 10^6 seats, and the page can ask about 10^5 of them. Walking through the hall for each question costs up to 10^6 steps, which is 10^11 steps in all. A seat number holds its row and column as arithmetic, so each question can cost a constant number of steps.