Row seating scout
EasyOnline Assessment
Pick the best row in a cinema for a group that wants to sit together.
Each string in rows describes one row of seats from left to right, with . for a free seat, X for a taken seat and | for an aisle. A group of group people fits in a row when it can sit in group consecutive free seats: no taken seat and no aisle may fall between or among them. A placement is identified by the seat where it starts, so overlapping placements are different placements (a free run of 4 seats allows 3 placements of a group of 2). Count the placements in each row.
The best row is the one with the most placements. If several rows tie, the best is the one listed first (rows are listed from the front of the cinema).
Input: rows, a list of strings, and group, a positive integer.
Output: an integer, the 0-based index of the best row. If no row has even one placement (including when rows is empty), return -1.
Example 1
- Input:
- [["..X..","X...."],2]
- Output:
- 1
- Explanation
- A group of 2 fits in row 0 in 2 ways (the two free pairs
..) and in row 1 in 3 ways (positions 1-2, 2-3, 3-4 of the run....). Row 1 has more placements, so the answer is 1.
Example 2
- Input:
- [["...|...",".....X.","X.....X"],3]
- Output:
- 1
- Explanation
- Row 0 is split by the aisle into two blocks of 3, giving 1 + 1 = 2 placements (ignoring the aisle would wrongly give 5). Rows 1 and 2 each have a run of 5 free seats, which gives 3 placements each. Rows 1 and 2 tie, and the front-most one, row 1, wins.
Example 3
- Input:
- [["XX.","|.X"],2]
- Output:
- -1
- Explanation
- Every row is too crowded or split too finely to hold 2 seats in a row, so no row can seat the group:
-1.
Constraints
0 <= rows.length <= 2000 <= rows[i].length <= 100; rows may have different lengthsrows[i]contains only.,Xand|1 <= group <= 100