Advertisement
kosievdmerwe

Untitled

Mar 5th, 2022
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. MOD = 10 ** 9 + 7
  2.  
  3. class Solution:
  4.     def countOrders(self, n: int) -> int:
  5.         # Answer is (2n)! / 2^n
  6.         # as we can consider all the distinct shufflings of the array [1, 1, 2, 2, ..., n, n]
  7.         # The first occurrence of a number is it's pickup, the second is it's drop-off
  8.        
  9.         # Calculating this number modulu MOD, is annoying, but we can simplify this into
  10.         # n! * (1 * 3 * ... * (2n - 1)) % MOD
  11.  
  12.         return functools.reduce(
  13.             lambda a, b: (a * b) % MOD,
  14.             itertools.chain(range(1, n + 1), range(1, 2 * n, 2)),
  15.         )
  16.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement