[Leetcode] 004. Median of two Sorted Arrays (Hard) (C++/java/python)

Source: Internet
Author: User

Index: [Leetcode] leetcode key index (C++/JAVA/PYTHON/SQL)
Github:https://github.com/illuz/leetcode


004.median_of_two_sorted_arrays (Hard) link :

Title: https://oj.leetcode.com/problems/Median-of-Two-Sorted-Arrays/
Code (GitHub): Https://github.com/illuz/leetcode :

Find the median of two ordered arrays. Required complexity is O (log (n + m)). Analysis :

Two ways of thinking:
1. Direct merge two arrays, then the median, can be passed, but the complexity is O (n + m).
2. To do it with a two-point mentality, this is not a good idea, but also to consider the parity. Can be converted into thinking, to find two ordered array of K large number, so it is better to think of. code :

1. Merge

C++:

Class Solution {public
:
	double findmediansortedarrays (int a[], int m, int b[], int n) {
		vector<int> c;< C3/>int pa = 0, pb = 0;	Point of A & B while

		(PA < m | | PB < n) {
			if (Pa = = m) {
				c.push_back (b[pb++]);
				Continue;
			}
			if (PB = N) {
				c.push_back (a[pa++]);
				Continue;
			}
			if (A[pa] > B[PB])
				c.push_back (b[pb++));
			else
				C.push_back (a[pa++]);
		}
		if ((n + m) &1) return
			c[(n+m)/2];
		else return
			(c[(n+m)/2-1] + c[(n+m)/2))/2.0;
	}
;


2. Two points

C++:

Class Solution {
	private:
		double findkthsortedarrays (int a[], int m, int b[], int n, int k) {
			if (M < n) { C3/>swap (n, m);
				Swap (A, B);
			}
			if (n = = 0) return
				a[k-1];
			if (k = = 1) return
				min (a[0], b[0]);

			int pb = min (K/2, n), PA = K-PB;
			if (A[pa-1] > B[pb-1]) return
				findkthsortedarrays (A, M, B + PB, N-PB, K-PB);
			else if (A[pa-1] < b[pb-1]) return
				findkthsortedarrays (A + PA, m-pa, B, N, k-pa);
			else return
				A[pa-1];
		}

	Public:
		double findmediansortedarrays (int a[], int m, int b[], int n) {
			if ((n + m) &1) return
				FINDKTHSO Rtedarrays (A, M, B, N, (n + m)/2 + 1);
			else return
				(Findkthsortedarrays (A, m, B, N, (n + m)/2 + 1) +
						findkthsortedarrays (A, m, B, N, (n + m)/2))/ 2.0;
		}
;


Java:

public class Solution {private double findkthsortedarrays (int a[], int astart, int aend,
        int b[], int bstart, int bend, int k) {int m = aend-astart, n = bend-bstart;
        if (M < n) {return findkthsortedarrays (B, bstart, bend, A, Astart, Aend, K);
        } if (n = = 0) return A[astart + k-1];

        if (k = = 1) return math.min (A[astart], B[bstart]);
        int PB = Math.min (K/2, n), PA = K-PB; if (A[astart + pa-1] > B[bstart + pb-1]) return to Findkthsortedarrays (A, Astart, Aend, B, bstart + PB, be
        nd, K-PB); else if (A[astart + Pa-1] < B[bstart + pb-1]) return findkthsortedarrays (A, Astart + PA, aend, B, Bstar
        T, Bend, K-PA);
    else return A[astart + pa-1];
        public double findmediansortedarrays (int a[], int b[]) {int m = a.length, n = b.length;
    if ((n + m)% 2 = 1)        Return Findkthsortedarrays (A, 0, M, B, 0, N, (n + m)/2 + 1); else return (findkthsortedarrays (A, 0, M, B, 0, N, (n + m)/2 + 1) + findkthsortedarrays (
    A, 0, M, B, 0, N, (n + m)/2)/2.0;
 }
}


Python:

Class Solution:
    def findkthsortedarrays (self, A, B, K):
        If Len (a) < Len (b):
            tmp = a
            a = b
            b = tmp
  if len (b) = = 0: return
            a[k-1]
        If k = 1: Return
            min (a[0], b[0])

        PB = Min (K/2, Len (B))
        pa = k- PB
        if a[pa-1] > b[pb-1]: Return
            self.findkthsortedarrays (A, B[PB:], K-PB)
        elif A[pa-1] < b[p B-1]: Return
            self.findkthsortedarrays (a[pa:], B, K-PA)
        else: return
            A[pa-1]

    # @return A float
  def findmediansortedarrays (self, A, b):
        if (Len (a) + len (B))% 2 = 1: Return
            self.findkthsortedarrays (A, B, (Len (a) + len (b))/2 + 1)
        else: Return
            (Self.findkthsortedarrays (A, B, (Len (a) + len (b))/2) +
                SELF.FINDK Thsortedarrays (A, B, (Len (a) + len (B))/2 + 1))/2.0



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.