Share my o(log(min(m,n)) solution with explanation | LeetCode Discuss



Share my o(log(min(m,n)) solution with explanation | LeetCode Discuss

To solve this problem, we need to understand "What is the use of median". In statistics, the median is used for dividing a set into two equal length subsets, that one subset is always greater than the other. If we understand the use of median for dividing, we are very close to the answer.

First let's cut A into two parts at a random position i:

      left_A             |        right_A  A[0], A[1], ..., A[i-1]  |  A[i], A[i+1], ..., A[m-1]  

Since A has m elements, so there are m+1 kinds of cutting( i = 0 ~ m ). And we know: len(left_A) = i, len(right_A) = m - i . Note: when i = 0 , left_A is empty, and when i = m , right_A is empty.

With the same way, cut B into two parts at a random position j:

      left_B             |        right_B  B[0], B[1], ..., B[j-1]  |  B[j], B[j+1], ..., B[n-1]  

Put left_A and left_B into one set, and put right_A and right_B into another set. Let's name them left_part and right_part :

      left_part          |        right_part  A[0], A[1], ..., A[i-1]  |  A[i], A[i+1], ..., A[m-1]  B[0], B[1], ..., B[j-1]  |  B[j], B[j+1], ..., B[n-1]  

If we can ensure:

1) len(left_part) == len(right_part)  2) max(left_part) <= min(right_part)  

then we divide all elements in {A, B} into two parts with equal length, and one part is always greater than the other. Then median = (max(left_part) + min(right_part))/2.

To ensure these two conditions, we just need to ensure:

(1) i + j == m - i + n - j (or: m - i + n - j + 1)      if n >= m, we just need to set: i = 0 ~ m, j = (m + n + 1)/2 - i  (2) B[j-1] <= A[i] and A[i-1] <= B[j]  

(For simplicity, I presume A[i-1],B[j-1],A[i],B[j] are always valid even if i=0/i=m/j=0/j=n . I will talk about how to deal with these edge values at last.)

So, all we need to do is:

Searching i in [0, m], to find an object `i` that:      B[j-1] <= A[i] and A[i-1] <= B[j], ( where j = (m + n + 1)/2 - i )  

And we can do a binary search following steps described below:

<1> Set imin = 0, imax = m, then start searching in [imin, imax]    <2> Set i = (imin + imax)/2, j = (m + n + 1)/2 - i    <3> Now we have len(left_part)==len(right_part). And there are only 3 situations       that we may encounter:      <a> B[j-1] <= A[i] and A[i-1] <= B[j]          Means we have found the object `i`, so stop searching.      <b> B[j-1] > A[i]          Means A[i] is too small. We must `ajust` i to get `B[j-1] <= A[i]`.          Can we `increase` i?              Yes. Because when i is increased, j will be decreased.              So B[j-1] is decreased and A[i] is increased, and `B[j-1] <= A[i]` may              be satisfied.          Can we `decrease` i?              `No!` Because when i is decreased, j will be increased.              So B[j-1] is increased and A[i] is decreased, and B[j-1] <= A[i] will              be never satisfied.          So we must `increase` i. That is, we must ajust the searching range to          [i+1, imax]. So, set imin = i+1, and goto <2>.      <c> A[i-1] > B[j]          Means A[i-1] is too big. And we must `decrease` i to get `A[i-1]<=B[j]`.          That is, we must ajust the searching range to [imin, i-1].          So, set imax = i-1, and goto <2>.  

When the object i is found, the median is:

max(A[i-1], B[j-1]) (when m + n is odd)  or (max(A[i-1], B[j-1]) + min(A[i], B[j]))/2 (when m + n is even)  

Now let's consider the edges values i=0,i=m,j=0,j=n where A[i-1],B[j-1],A[i],B[j] may not exist. Actually this situation is easier than you think.

What we need to do is ensuring that max(left_part) <= min(right_part). So, if i and j are not edges values(means A[i-1],B[j-1],A[i],B[j] all exist), then we must check both B[j-1] <= A[i] and A[i-1] <= B[j]. But if some of A[i-1],B[j-1],A[i],B[j] don't exist, then we don't need to check one(or both) of these two conditions. For example, if i=0, then A[i-1] doesn't exist, then we don't need to check A[i-1] <= B[j]. So, what we need to do is:

Searching i in [0, m], to find an object `i` that:      (j == 0 or i == m or B[j-1] <= A[i]) and      (i == 0 or j == n or A[i-1] <= B[j])      where j = (m + n + 1)/2 - i  

And in a searching loop, we will encounter only three situations:

<a> (j == 0 or i == m or B[j-1] <= A[i]) and      (i == 0 or j = n or A[i-1] <= B[j])      Means i is perfect, we can stop searching.    <b> j > 0 and i < m and B[j - 1] > A[i]      Means i is too small, we must increase it.    <c> i > 0 and j < n and A[i - 1] > B[j]      Means i is too big, we must decrease it.  


Read full article from Share my o(log(min(m,n)) solution with explanation | LeetCode Discuss


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts