4. Median of Two Sorted Arrays && 23. Merge k Sorted Lists

4. Median of Two Sorted Arrays

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
解题思路

大致思路是,分别求两个序列的中位数,比较——若相等,则该中位数即为合并后序列的中位数;若不相等,可以去掉较短序列的一半(若短序列的中位数较大,去掉更大的部分,因为这一部分合并后必然在中位数的右边,反之亦然),而较长序列去掉对应的相同长度的一部分。

如此,问题规模越来越小,而终止条件为:

若较短序列的长度只有1或者2,直接插入较长序列,然后求中位数即可。

程序代码
void insertX(vector <int> & nums1, int a, int b, int value){
    if (b == (int)nums1.size() - 1) nums1.push_back(1);
    int j = b;
    while (j >= a && nums1[j] > value) { nums1[j + 1] = nums1[j]; j--; }
    nums1[j + 1] = value;
}
double get_m(vector <int> & nums2, int a, int b){
    if ((b - a) % 2 == 0) return nums2[(a + b) / 2];
    return (nums2[(a + b) / 2] + nums2[(a + b) / 2 + 1]) * 1.0 / 2;
}
double fun(vector<int>& nums1, vector<int>& nums2, int a, int b, int c, int d){
    //two elements
    if (b - a == 1){
        insertX(nums2, c, d, nums1[a]);
        insertX(nums2, c, d + 1, nums1[a + 1]);
        return get_m(nums2, c, d + 2);
    }
    //one element
    if (b == a){
        insertX(nums2, c, d, nums1[a]);
        return get_m(nums2, c, d + 1);
    }
    double N1 = get_m(nums1, a, b), N2 = get_m(nums2, c, d);
    if (N1 == N2) return N2;
    else {
        int Y = (b - a) / 2; 
        if (N1 > N2) return fun(nums1, nums2, a, b - Y, c + Y, d); 
        else return fun(nums1, nums2, a + Y, b, c, d - Y);
    }
}
class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        if (nums1.size() == 0 ) return get_m(nums2, 0, nums2.size() - 1);
        if (nums2.size() == 0 ) return get_m(nums1, 0, nums1.size() - 1);
        if (nums1.size() <= nums2.size())return fun(nums1, nums2, 0, (int)nums1.size() - 1, 0, (int)nums2.size() - 1);
        return fun(nums2, nums1, 0, (int)nums2.size() - 1, 0, (int)nums1.size() - 1);
    }
};

23. Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Example:

Input:
[
  1->4->5,
  1->3->4,
  2->6
]
Output: 1->1->2->3->4->4->5->6
解题思路

法1:直接两两合并,效率低但是可以通过。

法2:每次取出最小的一个,使用尾插法连接即可。具体地,使用优先队列(小项堆)存储n个节点,每次取队头(即最小值),并往队列中插入该节点的下一个节点,遍历直到队列为空。

程序代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    //O(mlogn)
    struct compare{
        bool operator()(const ListNode* A, const ListNode * B){ return A->val > B->val; }
    };
    ListNode* mergeKLists(vector<ListNode*>& lists){
        ListNode * head = new ListNode(0);
        ListNode * r = head;
        priority_queue<ListNode*, vector<ListNode*>, compare> pq;
        for(int i = 0; i < lists.size(); i++) if (lists[i]) pq.push(lists[i]);
        while(!pq.empty()){
            ListNode * curMin = pq.top();
            ListNode * nextNode = curMin->next;
            pq.pop();
            if (nextNode) pq.push(nextNode);
            r->next = curMin;
            r = curMin;
        }
        return head->next;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值