EPI (Heap) Merge Multiple sorted arrays.

#include <vector>
#include <iostream>
#include <queue>
using namespace std;

/*
  Write a program that takes an input a set of sorted sequences and compute the union
  of these sequences as a sorted sequence.
  For example:
  [3, 5, 7], [0, 6, 8], [0, 6, 28], then the output is [0, 0, 3, 5, 6, 6, 7, 28]
*/
/*
  This problem can further be divided into two.
  1: There are only two sorted sets.
  2: The number of input sets are more than two.
*/
// Time complexity: O(N), Space O(N)
vector<int> mergeTwoSets(vector<int>& set_1, vector<int>& set_2) {
  int m = set_1.size();
  if(m == 0) return set_2;
  int n = set_2.size();
  if(n == 0) return set_1;
  vector<int> res;
  int i = 0, j = 0;
  while(i < m && j < n) {
    if(set_1[i] <= set_2[j]) {
      res.push_back(set_1[i++]);
    } else {
      res.push_back(set_2[j++]);
    }
  }
  while(i < m) {
    res.push_back(set_1[i++]);
  }
  while(j < n) {
    res.push_back(set_2[j++]);
  }
  return res;
}
void printVector(vector<int>& res) {
  for(int i = 0; i < res.size(); ++i) {
    cout << res[i] << endl;
  }
}

// Identical with merging multiple sorted lists. Time complexity: NlgK (K is the number of input size). Space lgK
vector<int> mergeMultiSets(const vector< vector<int> >& sets) {
  struct IteratorStartAndEnd {
    bool operator < (const IteratorStartAndEnd& that) const {
      return *start > *that.start;
    }
    vector<int>::const_iterator start;
    vector<int>::const_iterator end;
  };
  priority_queue< IteratorStartAndEnd, vector<IteratorStartAndEnd> > minHeap;
  for(const vector<int>& sorted_array : sets) {
    if(!sorted_array.empty())
    minHeap.push(IteratorStartAndEnd{sorted_array.cbegin(), sorted_array.cend()});
  }

  vector<int> res;
  while(!minHeap.empty()) {
    auto smallest_array = minHeap.top();
    minHeap.pop();
    if(smallest_array.start  != smallest_array.end) {
      res.push_back(*smallest_array.start);
      minHeap.push(IteratorStartAndEnd{next(smallest_array.start), smallest_array.end});
    }
  }
  return res;
}
int main(void) {
  vector<int> set_1{0, 15};
  vector<int> set_2{2, 3, 10, 12};
  vector<int> set_3{2, 3, 9, 14};
//  vector<int> res = mergeTwoSets(set_1, set_2);
  //printVector(res);
  vector< vector<int> > multisets;
  multisets.push_back(set_1);
  multisets.push_back(set_2);
  multisets.push_back(set_3);
  vector<int> res_1 = mergeMultiSets(multisets);
  printVector(res_1);
}

时间: 2024-10-14 10:35:50

EPI (Heap) Merge Multiple sorted arrays.的相关文章

Merge k Sorted Arrays

Given k sorted integer arrays, merge them into one sorted array. Given 3 sorted arrays: [ [1, 3, 5, 7], [2, 4, 6], [0, 8, 9, 10, 11] ] return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]. 这题和merge k sorted lists 一脉相承,但是处理起来要复杂些,即linked list可以非常容易的从当前结点通过.n

Lintcode6 Merge Two Sorted Arrays solution 题解

[题目描述] Merge two given sorted integer array A and B into a new sorted integer array. 合并两个排序的整数数组A和B变成一个新的数组. [题目链接] http://www.lintcode.com/en/problem/merge-two-sorted-arrays/ [题目解析] A和B都已经是排好序的数组,我们只需要从后往前比较就可以了. 因为A有足够的空间容纳A + B,我们使用游标i指向m + n - 1,

Merge Two Sorted Arrays

class Solution { /** * @param A and B: sorted integer array A and B. * @return: A new sorted integer array */ public int[] mergeSortedArray(int[] A, int[] B) { // Write your code here int n = A.length + B.length; int[] rst = new int[n]; int a = 0; in

[leetcode]Merge k Sorted Lists @ Python [基础知识: heap]

原题地址:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题意:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路: 归并k个已经排好序的链表, 使用堆这一数据结构. 堆,也叫做:priority queue 首先将每条链表的头节点进入堆中. 然后将最小的弹出,并将最小的节点这条链

[LeetCode] 23. Merge k Sorted Lists ☆☆

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解法1: 采用递归的方法,不管合并几个,归根到底还是需要两两合并. 首先想到的是前两个先合并,然后再跟第三个合并,然后第四个....但是这种做法效率不高. 换个思路,采用分治法,对数量超过2的任务进行拆分,直到最后只有一个或两个链表再进行合并.代码如下: /** * Definition for si

leetcode----------------Median of Two Sorted Arrays

There are two sorted arrays A and B 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)). 分析 这是一道非常经典的题.这题更通用的形式是,给定两个已经排序好的数组,找到两者所有元素中第 k 大的元素.O(m + n) 的解法比较直观,直接merge两个数组,然后

[email&#160;protected] find kth smallest element in two sorted arrays (O(log n time)

The trivial way, O(m + n): Merge both arrays and the k-th smallest element could be accessed directly. Merging would require extra space of O(m+n). The linear run time is pretty good, but could we improve it even further? A better way, O(k): There is

一起刷LeetCode4-Median of Two Sorted Arrays

实验室太吵了...怎么办啊... -------------------------------------------------------------------------------------------------------------------- Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the media

[leet code 4] Median of Two Sorted Arrays

1 题目 There are two sorted arrays A and B 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)). 2 思路 这题比较简单,实现的其实就是归并排序merge那个部分,另外,也让我知道了,算偶数 sum%2==0  与 sum/2==0是不一样的,后者在sum=1