552. Student Attendance Record II
Latest:
class Solution {
public int checkRecord(int n) {
Integer[][][] memo = new Integer[n][2][3];
return dfs(n, 0, 0, 0, memo);
}
private int dfs(int n, int i, int absentCount, int lateCount, Integer[][][] memo) {
if (absentCount >= 2 || lateCount >= 3) {
return 0;
}
if (i == n) {
return 1;
}
if (memo[i][absentCount][lateCount] != null) {
return memo[i][absentCount][lateCount];
}
// choose P, so no L only choose L can have consecutive case, a remain original value
long result = dfs(n, i+1, absentCount, 0, memo);
// choose A, so no L only choose L can have consecutive case, A+1
result += dfs(n, i+1, absentCount+1, 0, memo);
// choose L
// only choose L can have consecutive case, so lateCount can +1
result += dfs(n, i+1, absentCount, lateCount+1, memo);
return memo[i][absentCount][lateCount] = (int)(result%(1_000_000_007));
}
}
/***
A count < 2
L consecutive count < 3
[i][a count][l c count]
n. 3. 0 1 0 1 2
l = 0
how to handle " consecutive days."?
-> consecutive call dfs!
so other 2 DFS, l = 0
we only increase l count in consecutive call (means in the same DFS), other dfs give them l = 0
T: O(n*2*3) = O(n)
S: O(n)
practice, if I want all combinations count?? without any restrictions
private int dfsTest(int n, int i) {
if (i == n) {
return 1;
}
int result = 0;
for (int c = 0; c < 3; c++) {
result += dfsTest(n, i+1); //if no restrctions: f(n) = A(n-1) + L(n-1) + P(n-1) -> means for loop 3 times
}
return result;
}
or can write like this, the same position we can have 3 different choices, A or L or P
// A
result += dfsTest(n, i+1);
// L
result += dfsTest(n, i+1);
// P
result += dfsTest(n, i+1);
so if there are restrictions, need to add restrictions in these 3 DFS:
// choose P, so no L only choose L can have consecutive case, a remain original value
long result = dfs(n, i+1, absentCount, 0, memo);
// choose A, so no L only choose L can have consecutive case, A+1
result += dfs(n, i+1, absentCount+1, 0, memo);
// choose L
// only choose L can have consecutive case, so lateCount can +1
result += dfs(n, i+1, absentCount, lateCount+1, memo);
*/For all combinations: idea is here:
refer to:
and this
https://leetcode.com/problems/student-attendance-record-ii/solution/
DFS + memo
T : O(n), it's memoized to O(TOTAL * A * L), since A and L have fixed number which is 2 and 3, then the complexity becomes, O(TOTAL) = O(N)
S: O(n)
another one
Last updated
Was this helpful?