码迷,mamicode.com
首页 > 其他好文 > 详细

552. Student Attendance Record II

时间:2017-11-26 11:19:35      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:more   text   alc   ati   actually   stat   make   refers   end   

Problem refer: https://leetcode.com/problems/student-attendance-record-ii/description

// My solution:
// A good mathmatical derivation problem.
// But it makes me confused with the word: "more than two continuous ‘L‘ (late)".
// And finally find it actually represents "LLL".
// The last solution refers to the discuss: 
// https://discuss.leetcode.com/topic/86696/share-my-o-n-c-dp-solution-with-thinking-process-and-explanation

class Solution {
public:
    static const int N = 100000 + 1;
    static const int MOD = 1000000000 + 7;
    int P[N],L[N],A[N];
    int ans[N];
    
    int calc(int n) {
        if (ans[n]) return ans[n];
        calc(n-1);
        
        P[n] = (P[n-1] + A[n-1])%MOD + L[n-1];
        P[n] %= MOD;
        
        L[n] = ((P[n-1] + A[n-1])%MOD + A[n-2])%MOD + P[n-2];
        L[n] %= MOD;
        A[n] = (A[n-1] + A[n-2])%MOD + A[n-3];
        A[n] %= MOD;
        ans[n] = (P[n] + L[n])%MOD + A[n];
        ans[n]%=MOD;
        return ans[n];
    }
    int checkRecord(int n) {
        memset(ans, 0, sizeof ans);
        P[1]=L[1]=A[1]=1;
        ans[1] = 3;
    
        P[2] = L[2] = 3;
        A[2] = 2;
        ans[2] = 8;
    
        P[3] = 8;
        L[3] = 7;
        A[3] = 4;
        ans[3] = 19;
        return calc(n);
    }
};

 

552. Student Attendance Record II

标签:more   text   alc   ati   actually   stat   make   refers   end   

原文地址:http://www.cnblogs.com/kkrisen/p/7897923.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!