There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Idea1
If m+n is old (奇数), the median is the number located in position (m+n)/2 of the array (A+B)
If m+n is even (偶数), the median is the average of element in (m+n)/2 -1 and element in (m+n)/2
So you need to judge if the m+n is old or even. Then, The problem is find the kth biggest element in array(A+B).
After you decide the old and even of m+n, the idea of solution is to find the kth element in the array (A+B).
Idea2
The idea is like Merge operation in CLRS. Retrieve the minimal element in A[] and B[] until you reach the median (Maybe two numbers for even case). Complexity is O(m+n)
We need to carefully treat the cases when A[] is empty or B[] is empty at the beginning
Use two parameter "front" , "rear" to record two elements for even case. And "rear" for old case.
During the procedure to find the minimal element in A[]+B[]. You may meet the following cases
Either A[] or B[] become empty, we just use the other sequence
If A[] and B[] are non-empty, we will choose the smaller one.
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
Idea
Index always point to the last element without duplicates
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
In last several month, I tried to find an internship opportunity. After submitting 30+ resume, I just receive 2 replies. None of them are willing to hire me. Maybe, I am still too weak and too fool. I will keep moving on -_-!.
I use the following command to test KVM performance.