The trick here is to name the state correctly, then let the implementation follow. For 1349. Maximum Students Taking Exam, the solution in this repository is mainly a dynamic programming 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: dynamic programming, two pointers, bit manipulation.
The notes already sitting in the source point us in the right direction:
- DP, bitmask
- https://leetcode.com/problems/maximum-students-taking-exam/discuss/503686/A-simple-tutorial-on-this-bitmasking-problem
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are maxStudents, validity.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
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//DP, bitmask
02//https://leetcode.com/problems/maximum-students-taking-exam/discuss/503686/A-simple-tutorial-on-this-bitmasking-problem
03//Runtime: 4 ms, faster than 84.94% of C++ online submissions for Maximum Students Taking Exam.
04//Memory Usage: 8.1 MB, less than 100.00% of C++ online submissions for Maximum Students Taking Exam.
05class Solution {
06public:
07 int maxStudents(vector<vector<char>>& seats) {
08 int m = seats.size();
09 int n = seats[0].size();
10 //0th element for padding
11 vector<int> validity(m+1, 0);
12
13 for(int i = 1; i <= m; i++){
14 /*
15 rowValid has n bits,
16 if jth bit(from right to left) is 1,
17 jth seat(from left to right) is able to sit
18 */
19 int rowValid = 0;
20 for(int j = 0; j < n; j++){
21 rowValid += ((seats[i-1][j] == '.') << j);
22 }
23 validity[i] = rowValid;
24 }
25
26 //-1 means that state is invalid
27 vector<vector<int>> dp(m+1, vector(1 << n, -1));
28 /*
29 0th row is imaginary,
30 only 0(all seats are not available) is its valid state
31 */
32 dp[0][0] = 0;
33
34 //iterate through rows
35 for(int i = 1; i <= m; i++){
36 //get current row's validity
37 int valid = validity[i];
38
39 //iterate through all possible states of current row
40 /*
41 if a bit in j is 1, that bit is seated by a person
42 */
43 for(int j = 0; j < 1 << n; j++){
44 //there are two people seating adjacently
45 if(j & j >> 1) continue;
46 /*
47 bits in valid specify the positions of available seats,
48 bits in j specify the position of seated seat,
49 so j should be a subset of valid
50
51 e.g.
52 valid = 1011,
53 j should be 1010, 1001, 0011, ...,
54 but not 1111,
55 since 2nd bit(counting start from right)'s seat is broken
56 */
57 if((j & valid) != j) continue;
58 //now j is a valid state
59
60 //iterate through previous row's state
61 for(int k = 0; k < 1 << n; k++){
62 //invalid, not initialized
63 if(dp[i-1][k] == -1) continue;
64 //there's person in up-left or up-right position
65 if(j & k >> 1 || k & j >> 1) continue;
66 dp[i][j] = max(dp[i][j], dp[i-1][k] + __builtin_popcount(j));
67 }
68 }
69 }
70
71 return *max_element(dp[m].begin(), dp[m].end());
72 }
73};
Cost