Leetcode 1048 Solution

This article provides solution to leetcode question 1048 (clumsy-factorial)

https://leetcode.com/problems/clumsy-factorial

Solution

class Solution:
    def clumsy(self, N: int) -> int:
        ans = 0
        n = N
        while n >= 1:
            tmp = n
            if n >= 2:
                tmp *= n - 1
            if n >= 3:
                tmp //= n - 2
            if n < N:
                tmp = -tmp
            if n >= 4:
                tmp += n - 3
            ans += tmp
            n -= 4
        return ans