Ctrl K

Groups of Four at the Cinema

Medium
A cinema has rows rows numbered from 1, and each row has 10 seats numbered 1 to 10. Aisles run between seats 3 and 4 and between seats 7 and 8. The list taken holds the seats already sold: a pair [r, s] means seat s in row r is occupied. A group of four wants to sit together on four adjacent seats in one row. Seats on opposite sides of an aisle are not adjacent, with one exception: a group may sit so that an aisle splits it exactly in half, two people on each side. Return the largest number of groups that can be seated.
Example 1
rows=2, taken=[[1, 5], [2, 3], [2, 9]] → 2

In row 1 seat 5 is taken, which blocks the left half and the middle, but seats 6–9 are free: one group. In row 2 seats 3 and 9 are taken, leaving only seats 4–7 between the aisles: one more. Total 2.

Example 2
rows=3, taken=[[2, 1], [2, 10]] → 6

The edge seats 1 and 10 never get in the way, so each of the three rows fits two groups: 6 in total.

Example 3
rows=1, taken=[[1, 4], [1, 7]] → 0

Seats 4 and 7 cut through all three possible blocks, so no group fits.

Constraints
  • 1 ≤ rows ≤ 10⁹
  • 1 ≤ len(taken) ≤ min(10 · rows, 10⁴)
  • taken[i] = [r, s], 1 ≤ r ≤ rows, 1 ≤ s ≤ 10
  • All pairs in taken are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShifting Bits for Bigger Squares HardMost Different Pair MediumAll Non-Falling Subsequences MediumJobs With a Cooldown MediumSorted Into Runs of Three Medium
loading Python…
Input
rows=2, taken=[[1, 5], [2, 3], [2, 9]]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 77 tests on the server, hidden ones included.