1 Line Python in O(n)

  • 2
    def numWays(self, n, k):
        return 0 if n < 1 else sum(reduce(lambda F, i: [(k-1)*(F[0]+F[1]), F[0]], xrange(1, n), [k, 0]))

  • 0

    Nice. Could use sum(F) and n and, though (and _ and n-1 are clearer):

    return n and sum(reduce(lambda F, _: [(k-1)*sum(F), F[0]], xrange(n-1), [k, 0]))

    Granted, the n and makes it less clear :-)

Log in to reply

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