Subsequence: order, maynot continuous
QuickSort: Average O(NlgN), worst O(N2)
MergeSort: O(NlgN)
§Q
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
For example,
Given [3,2,1,5,6,4] and k = 2, return 5. [3,3,3,3] and k = 1, return 3.
§A
k starts with 1, Assume k is valid
-
sol1sort O(NlgN)
-
sol2 Max-Heap, O(Nlgk)
-
sol3 quick select
randomly choose/middle as privot
(…discard…pivot…index….)
time complexity: n/2 + n/4 + n/8 + … = n
duplicate -> just move it, don’t influence
worst case: all the number is same
1 | /** |
§Sort Color
- 0…0[l]1…1[r]2…2
1 | public void sortColors(int[] nums) { |
- What if 4 section
- 0, 1, [2,3]; then partition 2 & 3
- more pointers?
§Wiggle Sort I
Greedy
§Wiggle Sort II
Find Median: (nums.length+1) >>> 1, O(N)
Partition: sort color + Vitural Index, O(N)
1 | n = 6 => (n | 1) = 7 |
1 | public void wiggleSort2(int[] nums) { |
§expiry key hashmap in Java
How can I implement expiry key hashmap in Java?
1 | class ValueWithExpiration { |
-
follow up:
When get/put expirationTime?
Map => linear scan ? => PriorityQueue