Concise C++ solution


  • 0
    N

    Easy to understand C++ solution. Using F(n) = (n-1)*[F(n-1)+F(n-2)]

    public:
        int findDerangement(int n) {
            if(n==0||n==1)
                return 0;
            else {
                long long int a=1,b=0,ans;
                for(int i=0;i<=n;i++) {
                    ans = ((i-1)*(a+b))%1000000007;
                    b = a;
                    a = ans;
                }
                return ans;
            }
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.