Find the k-th Smallest Element in the Union of Two Sorted Arrays
(http://leetcode.com/2011/01/find-k-th-smallest-element-in-union-of.html)
Given two sorted arrays A, B of size m and n respectively. Find the k-th smallest element in the union of A and B. You can assume that there are no duplicate elements.
O(lg m + lg n) solution:
1. Maintaining the invariant
i + j = k - 1,
2. If Bj-1 < Ai < Bj, then Ai must be the k-th smallest,
or else if Ai-1 < Bj < Ai, then Bj must be the k-th smallest.
code:
int findKthSmallest(int A[], int m, int B[], int n, int k)
{
assert(A && m >= && B && n >= && k > && k <= m+n); int i = (int)((double)m / (m+n) * (k-));
int j = (k-) - i; assert(i >= && j >= && i <= m && j <= n); int Ai_1 = ((i == ) ? INT_MIN : A[i-]);
int Bj_1 = ((j == ) ? INT_MIN : b[j-]);
int Ai = ((i == m) ? INT_MAX : A[i]);
int Bj = ((j == n) ? INT_MAX : B[j]); if (Bj_1 < Ai && Ai < Bj)
return Ai;
else if (Ai_1 < Bj && Bj < Ai)
return Bj; assert((Ai > Bj && Ai_1 > Bi) || (Ai < Bj && Ai < Bj_1)); if (Ai < Bj)
return findKthSmallest(A+i+, m-i-, B, j, k-i-);
else
return findKthSmallest(A, i, B+j+, n-j-, k-j-);
}
Find the k-th Smallest Element in the Union of Two Sorted Arrays的更多相关文章
- 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)
转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- [LeetCode] Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- [Swift]LeetCode230. 二叉搜索树中第K小的元素 | Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- [Swift]LeetCode378. 有序矩阵中第K小的元素 | Kth Smallest Element in a Sorted Matrix
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- 230. Kth Smallest Element in a BST 找到bst中的第k小的元素
[抄题]: Given a binary search tree, write a function kthSmallest to find the kth smallest element in i ...
- LeetCode OJ:Kth Smallest Element in a BST(二叉树中第k个最小的元素)
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- [LeetCode] 378. Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
随机推荐
- Microsoft Azure 大计算 – 宣布收购 GreenButton
数据以及令人不可思议的计算能力,正在改变我们日常业务的经营方式,从科学和工程到媒体和金融,各行各业的客户正逐渐意识到什么是可能的.我们对整个基因组进行分析,以研制新药物.我们构建金融和保险模型, ...
- storage theory
preface/prehight:topic: Storage(share fileSystem(可共享文件系统,Access I/O existence bottleNeck,access read ...
- yum subversion puppet puppet-server
yum -y install ruby ruby-libs ruby-shadow yum -y install puppet puppet-server facter yum -y install ...
- 链表-remove duplicates from sorted list
struct ListNode* deleteDuplicates(struct ListNode* head) { struct ListNode *p=head; if(!head) return ...
- 2014 Web开发趋势
本文翻译自:http://www.pixelstech.net/article/1401629232-Web-design-trends-for-2014 如今,已然到了Web横行的时代.越来越多的资 ...
- poj2388 高速排序 模板题
/** \brief poj2388 * * \param date 2014/8/5 * \param state AC * \return memory time * qsort 784K 110 ...
- BZOJ 2716 Violet 3 天使玩偶 CDQ分治
题目大意:初始给定平面上的一个点集.提供两种操作: 1.将一个点增加点集 2.查询距离一个点最小的曼哈顿距离 K-D树是啥...不会写... 我仅仅会CDQ分治 对于一个询问,查询的点与这个点的位置关 ...
- Javascript 基础编程练习一
Javascript 基础互动编程,这篇练习结合了function 函数名(), onclick 时间, prompt输入窗口, window.open和confirm窗口, 任务 1.新窗口打开时弹 ...
- 一个给力的html5 画多边形的例子
只需改变参数就能画出你想要的多边形,代码简单!不得不惊叹canvas的强大! 代码奉上 <!doctype html> <html> <head> <meta ...
- codevs 1455 路径 计算m^n%p
题目链接 题目描述 Description 小明从A1到An+1,他知道从A1到A2,从A2到A3,......,从An到An+1都有m条路,且从A1到An+1都只有这些路.小明想知道,从A1地到An ...