Test cases
|
|
Solution 1: accepted 2ms
DP: do we really need to create another grid? Nope.
Time: O(mn)
Space: O(2mn)
|
|
Solution 2: accepted 2ms
Time: O(mn)
Space: O(mn)
|
|
|
|
DP: do we really need to create another grid? Nope.
Time: O(mn)
Space: O(2mn)
|
|
Time: O(mn)
Space: O(mn)
|
|
Two-dimension DP: For ways to current position, w(current), equals to ways to its top plus ways to its left, w(current) = w(top) + w(left).
Time: O(mn)
Space: O(mn)
|
|
One-dimension DP.
DP.
Time: O(n)
Space: O(n) (extra space 0)
Brute force with low efficiency.
Q: How to add a new digit at the beginning of an array efficiently?
|
|
No array manipulation.
|
|
|
|
The performance is good, but the style is a diseaster man.
Time: O(n)
Space: O(n)
|
|
Looks better, but worse performance than solution 1.
|
|
|
|
Binary search.
|
|
Newton method. Can also be used on other polynomial equations (please save my poor math).
Mathmatical reference: http://www.matrix67.com/blog/archives/361
|
|
Time: O(log(m*n)) = O(logm + logn)
Space: O(1)
|
|
|
|
Time: O(2^n)
I forget to remove “static” on ways and it accumulates the results of all test cases.
|
|
A better alternative
Time: O(n)
Straight fibonacci: f(n) = f(n-1) + f(n-2)
|
|
|
|
Brute force with 3 loops, time complexity O(n^3)
Two-pointer after sorting.
|
|