[LeetCode 4] Median of Two Sorted arrays of different sizes (hard)

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: Binary Search

  1. 要找median倍啥,原始的方法是把兩個arrays合并汇四,然后找中位數(shù)匆瓜。但是Time ComplexityO(m + n)
  2. 那么O(log (m+n))的要求,就肯定是binary search羡棵, 思路如下
    image.png
image.png
class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        
        //1. Binary search: to find partition x &  partition Y, such that
        // 
        // 1) partition x + partition y = (xLen + yLen + 1) / 2, means element number of left half == element number of right half
        // 2) all elements from two arrays left half <= all elements from two arrays right half
        // x1, x2,| x3, x4, x5, x6, 
        // y1, y2, y3, y4, y5,| y6, y7, y8
        // partition x = 2, partition y = 5 ==> left half has 5 elements, right half has 5 elements
        // and make sure x2 <= y6 && y5 <=x3  (coz it's sorted array, therefore already guaranteed x2 <= x3, y5 <= y6)
        
        if (nums1.length > nums2.length) {
            return findMedianSortedArrays (nums2, nums1);
        }
        
        int nums1Len = nums1.length;
        int nums2Len = nums2.length;
        int halfLen = (nums1Len + nums2Len + 1) / 2;
        
        int start = 0;
        int end = nums1Len;
        int partitionX = 0;
        int partitionY = 0;
        
        while (start <= end) {
            partitionX = start + (end - start) / 2;
            partitionY = halfLen - partitionX;
            
            // if partitionX == 0, means there is nothing on the left, use MIN_VALUE instead
            int maxLeftX = partitionX == 0 ? Integer.MIN_VALUE : nums1 [partitionX - 1];
            int maxLeftY = partitionY == 0 ? Integer.MIN_VALUE : nums2 [partitionY - 1];
            
            // if partitionX == nums1 Len, means there is nothing on the right, use MAX_VALUE instead
            int minRightX = partitionX == nums1Len ? Integer.MAX_VALUE : nums1 [partitionX];
            int minRightY = partitionY == nums2Len ? Integer.MAX_VALUE : nums2 [partitionY];
             
            if (maxLeftX <= minRightY && maxLeftY <= minRightX) {
                
                // if combined array len is even
                if ((nums1Len + nums2Len) % 2 == 0) {
                    //System.out.println ("1+++++" + "x: " + partitionX + " Y: " + partitionY);
                    return (Math.max (maxLeftX, maxLeftY) + Math.min (minRightX, minRightY)) / 2.0;
                }

                // if it's odd
                //System.out.println ("2+++++" + "x: " + partitionX + " Y: " + partitionY);
                return (Math.max (maxLeftX, maxLeftY));    
            } else if (maxLeftX > minRightY) {
                end = partitionX - 1;
            } else if (maxLeftY > minRightX) {
                start = partitionX + 1;
            }
        }
        
        return -1;
        
        
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末瓢喉,一起剝皮案震驚了整個濱河市戚绕,隨后出現(xiàn)的幾起案子鉴逞,更是在濱河造成了極大的恐慌,老刑警劉巖偎痛,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件旱捧,死亡現(xiàn)場離奇詭異,居然都是意外死亡踩麦,警方通過查閱死者的電腦和手機枚赡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谓谦,“玉大人贫橙,你說我怎么就攤上這事》粗啵” “怎么了卢肃?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長才顿。 經(jīng)常有香客問我莫湘,道長,這世上最難降的妖魔是什么郑气? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任逊脯,我火速辦了婚禮,結(jié)果婚禮上竣贪,老公的妹妹穿的比我還像新娘。我一直安慰自己巩螃,他們只是感情好演怎,可當(dāng)我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著避乏,像睡著了一般爷耀。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上拍皮,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天歹叮,我揣著相機與錄音,去河邊找鬼铆帽。 笑死咆耿,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的爹橱。 我是一名探鬼主播萨螺,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了慰技?” 一聲冷哼從身側(cè)響起椭盏,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎吻商,沒想到半個月后掏颊,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡艾帐,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年乌叶,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片掩蛤。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡枉昏,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出揍鸟,到底是詐尸還是另有隱情兄裂,我是刑警寧澤,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布阳藻,位于F島的核電站晰奖,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏腥泥。R本人自食惡果不足惜匾南,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蛔外。 院中可真熱鬧蛆楞,春花似錦、人聲如沸夹厌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽矛纹。三九已至臂聋,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間或南,已是汗流浹背孩等。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留采够,地道東北人肄方。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像蹬癌,于是被迫代替她去往敵國和親扒秸。 傳聞我的和親對象是個殘疾皇子播演,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,047評論 2 355

推薦閱讀更多精彩內(nèi)容