|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +/** |
| 9 | + * 1386. Cinema Seat Allocation |
| 10 | + * |
| 11 | + * A cinema has n rows of seats, numbered from 1 to n and there are ten seats in each row, labelled from 1 to 10 as shown in the figure above. |
| 12 | + * Given the array reservedSeats containing the numbers of seats already reserved, for example, reservedSeats[i]=[3,8] means the seat located in row 3 and labelled with 8 is already reserved. |
| 13 | + * Return the maximum number of four-person families you can allocate on the cinema seats. A four-person family occupies fours seats in one row, that are next to each other. |
| 14 | + * Seats across an aisle (such as [3,3] and [3,4]) are not considered to be next to each other, however, It is permissible for the four-person family to be separated by an aisle, but in that case, exactly two people have to sit on each side of the aisle. |
| 15 | + * |
| 16 | + * Example 1: |
| 17 | + * Input: n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]] |
| 18 | + * Output: 4 |
| 19 | + * Explanation: The figure above shows the optimal allocation for four families, where seats mark with blue are already reserved and contiguous seats mark with orange are for one family. |
| 20 | + * |
| 21 | + * Example 2: |
| 22 | + * Input: n = 2, reservedSeats = [[2,1],[1,8],[2,6]] |
| 23 | + * Output: 2 |
| 24 | + * |
| 25 | + * Example 3: |
| 26 | + * Input: n = 4, reservedSeats = [[4,3],[1,4],[4,6],[1,7]] |
| 27 | + * Output: 4 |
| 28 | + * |
| 29 | + * Constraints: |
| 30 | + * 1 <= n <= 10^9 |
| 31 | + * 1 <= reservedSeats.length <= min(10*n, 10^4) |
| 32 | + * reservedSeats[i].length == 2 |
| 33 | + * 1 <= reservedSeats[i][0] <= n |
| 34 | + * 1 <= reservedSeats[i][1] <= 10 |
| 35 | + * All reservedSeats[i] are distinct. |
| 36 | + * */ |
| 37 | +public class _1386 { |
| 38 | + public static class Solution1 { |
| 39 | + public int maxNumberOfFamilies(int n, int[][] reservedSeats) { |
| 40 | + Map<Integer, Set<Integer>> map = new HashMap<>(); |
| 41 | + for (int[] seat : reservedSeats) { |
| 42 | + if (!map.containsKey(seat[0])) { |
| 43 | + map.put(seat[0], new HashSet<>()); |
| 44 | + } |
| 45 | + map.get(seat[0]).add(seat[1]); |
| 46 | + } |
| 47 | + int count = (n - map.size()) * 2; |
| 48 | + for (int key : map.keySet()) { |
| 49 | + Set<Integer> reservedOnes = map.get(key); |
| 50 | + if (reservedOnes.size() > 6) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + if (!reservedOnes.contains(2) && !reservedOnes.contains(3) && !reservedOnes.contains(4) && !reservedOnes.contains(5) && !reservedOnes.contains(6) && !reservedOnes.contains(7) && !reservedOnes.contains(8) && !reservedOnes.contains(9)) { |
| 54 | + count += 2; |
| 55 | + } else if (!reservedOnes.contains(4) && !reservedOnes.contains(5) && !reservedOnes.contains(6) && !reservedOnes.contains(7)) { |
| 56 | + count++; |
| 57 | + } else if (!reservedOnes.contains(2) && !reservedOnes.contains(3) && !reservedOnes.contains(4) && !reservedOnes.contains(5)) { |
| 58 | + count++; |
| 59 | + } else if (!reservedOnes.contains(6) && !reservedOnes.contains(7) && !reservedOnes.contains(8) && !reservedOnes.contains(9)) { |
| 60 | + count++; |
| 61 | + } |
| 62 | + } |
| 63 | + return count; |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments