kth sum of two sorted arrays
来源:互联网 发布:哈工大软件下载 编辑:程序博客网 时间:2024/05/23 01:26
转自:http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs;action=display;num=1132204952;start=50
一个解法可以是
q : priority queue (decreasing) := empty priority queueadd (0, 0) to q with priority a[0] + b[0]while k > 0: k-- x := pop q output x (i, j) : tuple of int,int := position of x if i < m: add (i + 1, j) to q with priority a[i + 1] + b[j] if j < n: add (i, j + 1) to q with priority a[i] + b[j + 1]
- The loop is executed k times.
- There is one pop operation per iteration.
- There are up to two insert operations per iteration.
- The maximum size of the priority queue is
O(min(m, n))O(m + n).- The priority queue can be implemented with a binary heap giving log(size) pop and insert.
- Therefore this algorithm is
O(k * log(min(m, n)))O(k * log(m + n)).
但注意可能有重复的值被添加,所以在填入q中时要先判断是否已存在,出队时删除其存在。用一个set可以方便的判断。
- kth sum of two sorted arrays
- LeetCode Week1: Two Sum、Add Two Numbers、Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of two sorted arrays
- Intersection of two sorted arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- median-of-two-sorted-arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- FTP, SFTP, FTPS examples In Java .
- 健康
- java配置环境变量
- Java Set 深入研究
- Hibernate中Criteria的完整用法
- kth sum of two sorted arrays
- 修改Ubuntu默认shell为bash
- Error: could not open `F:\Program Files\Java\jre6\lib\i386\jvm.cfg'
- vs2010-MSDN安装步骤
- CTeX套装自带的TeXworks编辑器使用简说
- 三星Android210评估板Android4.0源码
- python 系统学习笔记(五)---字典
- source insight 保存时删除多余空格,去除多余空格 space tab键
- AfxBeginThread和CreateThread具体区别