LeetCode Algorithm 0004 - Median of Two Sorted Arrays (Hard)

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/darkrabbit/article/details/82723432

LeetCode Algorithm 0004 - Median of Two Sorted Arrays (Hard)

Problem Link: https://leetcode.com/problems/median-of-two-sorted-arrays/description/


Description

There are two sorted arrays nums1 and nums2 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)).

You may assume nums1 and nums2 cannot be both empty.

Example 1:

nums1 = [1, 3]
nums2 = [2]

The median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5

Solution C++

#pragma once

#include "pch.h"

// Problem: https://leetcode.com/problems/median-of-two-sorted-arrays/description/

namespace P4MedianOfTwoSortedArrays
{
    class Solution
    {
        public:
        double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2)
        {
#if false // time O( (m+n)/2+1 ), space O( (m+n)/2+1 )
            int lSize = nums1.size();
            int rSize = nums2.size();
            int li = 0, ri = 0;
            int len = (lSize + rSize) / 2 + 1;
            vector<int> n = vector<int>(len);

            for (int i = 0; i < len; i++)
            {
                if (li < lSize && ri < rSize)
                {
                    n[i] = nums1[li] < nums2[ri] ? nums1[li++] : nums2[ri++];
                }
                else if (li >= lSize)
                {
                    n[i] = nums2[ri++];
                }
                else if (ri >= rSize)
                {
                    n[i] = nums1[li++];
                }
            }

            return (lSize + rSize) % 2 == 0 ? (n[len - 1] + n[len - 2]) / 2.0 : n[len - 1];
#endif

#if true // time O( log(min(m,n)) )
            int m = nums1.size();
            int n = nums2.size();
            if (m > n)
            { 
                // to ensure m<=n
                vector<int> temp = nums1; 
                nums1 = nums2; 
                nums2 = temp;
                int tmp = m; 
                m = n; 
                n = tmp;
            }

            int iMin = 0, iMax = m, halfLen = (m + n + 1) / 2;
            while (iMin <= iMax)
            {
                cout << "iMin=" << iMin << " iMax=" << iMax << endl;
                int i = (iMin + iMax) / 2; // nums1 medium
                int j = halfLen - i; // nums2 medium
                if (i < iMax && nums2[j - 1] > nums1[i])
                {
                    iMin = i + 1;
                }
                else if (i > iMin && nums1[i - 1] > nums2[j])
                {
                    iMax = i - 1;
                }
                else
                { 
                    int maxLeft = 0;
                    if (i == 0)
                    {
                        maxLeft = nums2[j - 1];
                    }
                    else if (j == 0)
                    {
                        maxLeft = nums1[i - 1];
                    }
                    else
                    {
                        maxLeft = nums1[i - 1] > nums2[j - 1] ? nums1[i - 1] : nums2[j - 1];
                    }
                    if ((m + n) % 2 == 1)
                    {
                        return maxLeft;
                    }

                    int minRight = 0;
                    if (i == m)
                    {
                        minRight = nums2[j];
                    }
                    else if (j == n)
                    {
                        minRight = nums1[i];
                    }
                    else
                    {
                        minRight = nums1[i] < nums2[j] ? nums1[i] : nums2[j];
                    }

                    return (maxLeft + minRight) / 2.0;
                }
            }
            return 0.0;
#endif
        }
    };
}

猜你喜欢

转载自blog.csdn.net/darkrabbit/article/details/82723432