Monday, September 21, 2020

LeetCode [552] Student Attendance Record II

 552. Student Attendance Record II

Hard

Given a positive integer n, return the number of all possible attendance records with length n, which will be regarded as rewardable. The answer may be very large, return it after mod 109 + 7.

A student attendance record is a string that only contains the following three characters:

  1. 'A' : Absent.
  2. 'L' : Late.
  3. 'P' : Present.

A record is regarded as rewardable if it doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late).

Example 1:

Input: n = 2
Output: 8 
Explanation:
There are 8 records with length 2 will be regarded as rewardable:
"PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL"
Only "AA" won't be regarded as rewardable owing to more than one absent times. 

Note: The value of n won't exceed 100,000.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
    int M = 1000000007;
    public int checkRecord(int n) {
        int endA = count(n, 1, 1, '*', 'A', true);
        int endP = count(n, 1, 1, '*', 'P', false);
        int endL = count(n, 1, 1, '*', 'L', false);
        return (((endA+endL)%M+endP)%M);
    }

    //[0..(i-1)] ending with "ab", compute [0..i]
    //cnt : the count for [0..(i-1)] ending with "ab"
    int count(int n, int i, int cnt, char a, char b, boolean hasA){
        if(i==n) return cnt;
        int endA = 0, endL = 0, endP = 0;
        endP = count(n, i+1, cnt, b, 'P', hasA);
        if(!hasA){
            endA = count(n, i+1, cnt, b, 'A', true);
        }
        if(!(a=='L' && b=='L')){
            endL = count(n, i+1, cnt, b, 'L', hasA);
        }
        return (((endA+endL)%M+endP)%M);
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution {
    int M = 1000000007;
    public int checkRecord(int n) {
        if(n==0) return 0;
        if(n==1) return 3;
        if(n==2) return 8;

        //initiate from n==2
        Map<String, Integer> map = new HashMap<>();//does not have A
        map.put("LL", 1);
        map.put("LP", 1);
        map.put("PL", 1);
        map.put("PP", 1);

        Map<String, Integer> mapA = new HashMap<>();//has A
        mapA.put("LA", 1);
        mapA.put("PA", 1);
        mapA.put("AL", 1);
        mapA.put("AP", 1);

        mapA.put("LL", 0);
        mapA.put("LP", 0);
        mapA.put("PL", 0);
        mapA.put("PP", 0);

        
        for(int i=2; i<n; ++i){
            Map<String, Integer> map_t = new HashMap<>();
            Map<String, Integer> mapA_t = new HashMap<>();
            map_t.put("LL", map.get("PL"));
            map_t.put("LP", (map.get("LL")+map.get("PL"))%M);
            map_t.put("PL", (map.get("LP")+map.get("PP"))%M);
            map_t.put("PP", (map.get("LP")+map.get("PP"))%M);

            mapA_t.put("LA", (map.get("LL")+map.get("PL"))%M);
            mapA_t.put("PA", (map.get("LP")+map.get("PP"))%M);
            mapA_t.put("AL", (mapA.get("LA")+mapA.get("PA"))%M);
            mapA_t.put("AP", (mapA.get("LA")+mapA.get("PA"))%M);

            mapA_t.put("LL", (mapA.get("AL")+mapA.get("PL"))%M);
            mapA_t.put("LP", ((mapA.get("LL")+mapA.get("PL"))%M+mapA.get("AL"))%M);
            mapA_t.put("PL", ((mapA.get("LP")+mapA.get("PP"))%M+mapA.get("AP"))%M);
            mapA_t.put("PP", ((mapA.get("LP")+mapA.get("PP"))%M+mapA.get("AP"))%M);


            map = map_t;
            mapA = mapA_t;
        }

        int r = 0;
        for(int v : map.values()){
            r = (r+v)%M;
        }
        for(int v : mapA.values()){
            r = (r+v)%M;
        }
        return r;
    }
}

No comments:

Post a Comment