|
| 1 | +/** |
| 2 | + * 1386. Cinema Seat Allocation |
| 3 | + * https://leetcode.com/problems/cinema-seat-allocation/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * A cinema has n rows of seats, numbered from 1 to n and there are ten seats in each row, labelled |
| 7 | + * from 1 to 10 as shown in the figure above. |
| 8 | + * |
| 9 | + * Given the array reservedSeats containing the numbers of seats already reserved, for example, |
| 10 | + * reservedSeats[i] = [3,8] means the seat located in row 3 and labelled with 8 is already reserved. |
| 11 | + * |
| 12 | + * Return the maximum number of four-person groups you can assign on the cinema seats. A four-person |
| 13 | + * group occupies four adjacent seats in one single row. Seats across an aisle (such as [3,3] and |
| 14 | + * [3,4]) are not considered to be adjacent, but there is an exceptional case on which an aisle |
| 15 | + * split a four-person group, in that case, the aisle split a four-person group in the middle, which |
| 16 | + * means to have two people on each side. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * @param {number} n |
| 21 | + * @param {number[][]} reservedSeats |
| 22 | + * @return {number} |
| 23 | + */ |
| 24 | +var maxNumberOfFamilies = function(n, reservedSeats) { |
| 25 | + const rowReservations = new Map(); |
| 26 | + |
| 27 | + for (const [row, seat] of reservedSeats) { |
| 28 | + if (!rowReservations.has(row)) { |
| 29 | + rowReservations.set(row, new Set()); |
| 30 | + } |
| 31 | + rowReservations.get(row).add(seat); |
| 32 | + } |
| 33 | + |
| 34 | + let result = 2 * (n - rowReservations.size); |
| 35 | + |
| 36 | + for (const seats of rowReservations.values()) { |
| 37 | + let canPlaceGroup = false; |
| 38 | + |
| 39 | + if (!seats.has(2) && !seats.has(3) && !seats.has(4) && !seats.has(5)) { |
| 40 | + result++; |
| 41 | + canPlaceGroup = true; |
| 42 | + } |
| 43 | + if (!seats.has(6) && !seats.has(7) && !seats.has(8) && !seats.has(9)) { |
| 44 | + result++; |
| 45 | + canPlaceGroup = true; |
| 46 | + } |
| 47 | + if (!canPlaceGroup && !seats.has(4) && !seats.has(5) && !seats.has(6) && !seats.has(7)) { |
| 48 | + result++; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return result; |
| 53 | +}; |
0 commit comments