Test cases
|
|
Solution 1: tle
|
|
|
|
|
|
In case we need to allow k duplicates, we only change the maximum of show to k.
|
|
DFS.
|
|
|
|
How to modularize the problem:
Time: O(n) // worst case, when all elements are the same
|
|
The most standard binary search.
|
|
Time: O(n) 2*(m+n)
Space: O(n) m+n
Brute force.
|
|
KMP.
|
|
Neat but slower (28%) KMP.
|
|
Iterative method.
Time: O(n)
Space: O(n)
|
|
Divide and conquer recursive solution. Not good for this simple question, since the method consumes extra time, but it’s good for some more complicated questions.
Time complexity is O(n) as we visit each node only once.
Time: O(n)
Space: O(n)
|
|
Naive recursive solution, trivial.
Time: O(n)
Space: O(n)
|
|
Two pointers. Since the value could be duplicated, binary search is not the best shot.
|
|
Binary. Conversion needed to gain precious result. For instance, for number 5, mid will be 2, int 5/2 = 2, which is not what we expect.
Time: O(logn)
Space: O(1)
|
|