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 ...
随机推荐
- 运用JavaScript构建你的第一个Metro式应用程序(on Windows 8)(一)
原文 http://blog.csdn.net/zhangxin09/article/details/6784547 作者:Chris Sells 译: sp42 原文 包括 HTML.CSS 和 ...
- 字符串匹配——KMP算法
关于KMP算法的分析,我觉得这两篇博客写的不错: http://www.ruanyifeng.com/blog/2013/05/Knuth–Morris–Pratt_algorithm.html ht ...
- c#搭建服务端 简单中最高效的数据操作Linq (4)
.NET F 3.5之后提出的linq to sql 概念,大大的简化了对于数据对象的操作,可以通过简单的语法直接操作数据对象,如List,Linq to sql类 等等. 1.使用Linq to s ...
- 链表-Reverse Linked List II
[题目要求直接翻转链表,而非申请新的空间] 这道题的一个关键在于,当m=1时,需要翻转的链表段前没有其他的结点(leetcode的测试用例不含头结点),这个特例给解题带来了一点小小的困难.一个比较直观 ...
- 使用sphinx索引mysql数据
数据库表如下 mysql> select * from tb_account; +----+-------+------+ | id | name | age | +----+-------+- ...
- ebs清除并法管理器所清除的表
In this Document Goal Solution References Applies to: Oracle Concurrent Processing - Version 1 ...
- SPOJ GSS1 && GSS3 (无更新/更新单点,并询问区间最大连续和)
http://www.spoj.com/problems/GSS1/ 题意:无更新询问区间最大连续和. 做法:线段树每个节点维护sum[rt],maxsum[rt],lsum[rt],rsum[rt] ...
- 初探swift语言的学习笔记(闭包-匿名函数或block块代码)
使用Block的地方很多,其中传值只是其中的一小部分,下面介绍Block在两个界面之间的传值: 先说一下思想: 首先,创建两个视图控制器,在第一个视图控制器中创建一个UILabel和一个UIButto ...
- 期间值日期查询SQL
甘特图的数据时常需要 查询出开始时间-结束时间的记录数据 常常我们使用的是 Between and 或者 >= , <= 仔细一想 我们需要查询的数据是有落在这个期间的数据 ...
- Lc.exe已退出 代码为-1问题解决方法
对于用vs作为开发工具的同学来说,可能常常会碰到“Lc.exe已退出 代码为-1”的问题,造成这个结果的一般是因为加入了第三方的插件程序造成的,今天一一讲解如何解决. 工具/原料 vs各版本开发工具 ...