(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 < A< Bj, then Ai must be the k-th smallest,

or else if Ai-1 < B< 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的更多相关文章

  1. 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)

    转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素

    题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. Unix/Linux环境C编程入门教程(8) FreeBSD CCPP开发环境搭建

    1. FreeBSD是一种自由类Unix操作系统,是由经过BSD.386BSD和4.4BSD发展而来的类Unix的一个重要分支.FreeBSD拥有超过200名活跃开发者和上千名贡献者.FreeBSD被 ...

  2. 【MongoDB数据库】MongoDB 命令入门初探

    MongoDB是一款NoSql数据库,使用了"面向集合"(Collection-Oriented)原理,意思是数据被分组存储在数据集中,被称为一个集合(Collection).每一 ...

  3. C++编程技术之 异常处理(上)

    增强错误恢复能力是提高代码健壮性的最有力途径之一 之所以平时编写代码的时候不愿意去写错误处理,主要是由于这项工作及其无聊并可能导致代码膨胀,导致的结果就是本来就比较复杂的程序变得更加复杂.当然了,前面 ...

  4. inotify

    inotify,文件系统控制函数,通知机制: ioctl, io控制函数

  5. XmlSerializer

    XmlSerializer作用是将对象序列化到 XML 文档中和从 XML 文档中反序列化对象.XmlSerializer 使您得以控制如何将对象编码到 XML 中. 所在的命名空间:System.X ...

  6. C# OR/Mapping 数据处理模式学习

    为什么要提出O/R Mapping概念 程序语言已经由面向过程的模型全面转向为面向对象的模型,UML的出现更加革新了软件开发方法论.然而数据库模型却从未随着开发语言的进步而随之革新,仍然使用面向关系的 ...

  7. <转>LINQ To SQL 语法及实例大全

    一篇很全很强大的linq to sql 总结 来源:http://blog.csdn.net/pan_junbiao/article/details/7015633 目录(?)[-] LINQ to ...

  8. jQuery获取Select选择的Text(非表单元素)和 Value(表单元素)(转)

    jQuery获取Select选择的Text和Value: 语法解释: . $("#select_id").change(function(){//code...}); //为Sel ...

  9. SET ANSI_NULLS (Transact-SQL)

    指定在 SQL Server 2014 中与 Null 值一起使用等于 (=) 和不等于 (<>) 比较运算符时采用符合 ISO 标准的行为. 当 SET ANSI_NULLS 为 ON ...

  10. IOS8 设置TableView Separatorinset 分割线从边框顶端开始

    IOS8 设置TableView Separatorinset 分割线从边框顶端开始   在ios8上 [TableViewsetSeparatorInset:UIEdgeInsetsMake(0,0 ...