This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1386. Cinema Seat Allocation, the solution in this repository is mainly a binary search solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: binary search, two pointers, sliding window, greedy.
The notes already sitting in the source point us in the right direction:
- map
- unordered_map
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are maxNumberOfFamilies.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- A set is doing the membership or uniqueness work, which keeps the main loop readable.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//map
02//Runtime: 420 ms, faster than 39.39% of C++ online submissions for Cinema Seat Allocation.
03//Memory Usage: 76 MB, less than 100.00% of C++ online submissions for Cinema Seat Allocation.
04//unordered_map
05//Runtime: 380 ms, faster than 44.97% of C++ online submissions for Cinema Seat Allocation.
06//Memory Usage: 75.1 MB, less than 100.00% of C++ online submissions for Cinema Seat Allocation.
07class Solution {
08public:
09 int maxNumberOfFamilies(int n, vector<vector<int>>& reservedSeats) {
10 int ans = 0;
11
12 // sort(reservedSeats.begin(), reservedSeats.end());
13
14 map<int, set<int>> rows;
15
16 for(vector<int>& seat : reservedSeats){
17 // cout << seat[0] << " " << seat[1] << " | ";
18 if(seat[1] != 1 && seat[1] != 10)
19 rows[seat[0]].insert(seat[1]);
20 }
21 // cout << endl;
22
23 set<int> left = {2,3,4,5};
24 set<int> middle = {4,5,6,7};
25 set<int> right = {6,7,8,9};
26
27 ans += (n - rows.size())*2;
28
29 for(auto it = rows.begin(); it != rows.end(); it++){
30 int i = it->first;
31 set<int> row = it->second;
32 set<int> result;
33
34 bool filled = false;
35
36 set_intersection(left.begin(), left.end(), row.begin(), row.end(), inserter(result, result.begin()));
37 if(result.size() == 0){
38 ans++;
39 filled = true;
40 }
41 result.clear();
42
43 set_intersection(right.begin(), right.end(), row.begin(), row.end(), inserter(result, result.begin()));
44 if(result.size() == 0){
45 ans++;
46 filled = true;
47 }
48 result.clear();
49
50 if(!filled){
51 set_intersection(middle.begin(), middle.end(), row.begin(), row.end(), inserter(result, result.begin()));
52 if(result.size() == 0){
53 ans++;
54 filled = true;
55 }
56 result.clear();
57 }
58 }
59
60 return ans;
61 }
62};
Cost