A recent CodeSignal Challenge was to calculate 1000C500 (mod 1e9+7) and I got defeated =(
All my trials exceeded the time limit.. Here is the best JS solution by psr
, could anyone explain what happens in this line??? I learnt ES6 but got no idea about this syntax...
f[o = n + 1/k] = o in f
full solution for reference, please tell me to delete this if I violated any rule...
f = countWays = (n, k) => f[o = n + 1/k] = o in f
? f[o]
: k
? n && (f(--n, k) + f(n, k - 1)) % (1e9 + 7)
: 1
Top comments (1)
Thanks Barbar's comments in StackOverflow, I understand the algorithm now.
The algorithm can be rewritten as follows:
Here goes an example of 5C2
P.S. I got a few takeaways when investigating into this algorithm
Double declaration of function on the same line as a trick for recursion
Immediate use of a key with its value just assigned
Infinity can be used as a key of an object(!)
Syntax o in f checks if object f has the key o