Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c2fc277

Browse files
add 1386
1 parent f3ae25c commit c2fc277

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1386|[Cinema Seat Allocation](https://leetcode.com/problems/cinema-seat-allocation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1386.java) | |Medium|Array, Greedy|
1112
|1385|[Find the Distance Value Between Two Arrays](https://leetcode.com/problems/find-the-distance-value-between-two-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1385.java) | |Easy|Array|
1213
|1382|[Balance a Binary Search Tree](https://leetcode.com/problems/balance-a-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1382.java) | |Medium|Binary Search Tree|
1314
|1381|[Design a Stack With Increment Operation](https://leetcode.com/problems/design-a-stack-with-increment-operation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1381.java) | |Medium|Stack, Design|
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1386;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1386Test {
10+
private static _1386.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1386.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(4, solution1.maxNumberOfFamilies(3, new int[][]{
20+
{1, 2},
21+
{1, 3},
22+
{1, 8},
23+
{2, 6},
24+
{3, 1},
25+
{3, 10},
26+
}));
27+
}
28+
29+
@Test
30+
public void test2() {
31+
assertEquals(2, solution1.maxNumberOfFamilies(2, new int[][]{
32+
{2, 1},
33+
{1, 8},
34+
{2, 6},
35+
}));
36+
}
37+
38+
@Test
39+
public void test3() {
40+
assertEquals(4, solution1.maxNumberOfFamilies(4, new int[][]{
41+
{4, 3},
42+
{1, 4},
43+
{4, 6},
44+
{1, 7},
45+
}));
46+
}
47+
48+
}

0 commit comments

Comments
 (0)