Test cases
|
|
Solution 1: accepted 20ms
Time: O(n)
Space: O(n)
|
|
Solution 2: accepted 12ms
Time: O(n)
Space: O(n)
|
|
|
|
Time: O(n)
Space: O(n)
|
|
Time: O(n)
Space: O(n)
|
|
use nums[mid] > nums[hi]
rather than nums[mid] > nums[lo]
to avoid consideration of corner case, no rotation. If there’s no rotation and we say if (nums[mid] > nums[lo]) lo = mid
, we abandon the left side, which has the answer.
|
|
|
|
Time: O(n) // worst case when all elements are the same
|
|
Time: O(n^2), Space: O(1)
Mutation: Replacing string manipulation with int values, but it dones’t improve the performance.
中间��?��?
Time: O(2 n^2), Space: O(1)
avoid unnecessary check, but still O(n^2), failed the “aaaaaaaa…” test case.
We should be able to cut the time to half by check “aba” and “abba” in the same loop:
Amaing boost in performace simply because I changed the duplicated code block in solution 2 to method. Is this the truth?
Solution 1: ~100ms
Solution 2: ~6ms (without “len - i < output.length() / 2” it will be 10ms and will not be accepted)
|
|
Binary search.
Since nums[-1] and num[n] are negative infinite, there must be a peak in the middle. Consider four conditions that mid may have, we can narrow the scope by half each time.
|
|
Some binary thinking in the middle but not enough. We could make the time complexity closer to O(logn).
|
|
Solution 1: NC
Have problem with test cases:
4294967295 (11111111111111111111111111111111).
-2147483647 (1000 0000 0000 0000 0000 0000 0000 0000).
|
|
|
|
|
|
DP: To calculate the total gain after robbing house number N, we need to consider the accumulated stash of house N-2 or N-3. N-1 is not available as it will trigger the alarm.
Time: O(n)
Or convert to Space O(1) by storing two previous stashes in two int variables.
More elegant code.
|
|
Basicly hard code, not wise at all.
|
|
Concise and fast. A smart way to detect overflow.
|
|
Even more concise and faster! Jump out of the “box”!
|
|
In this question we need to rearrange a string according to certain order. We need to find out the order of rearrangement.
|
|
|
|