查找两个有序数组中的第K个元素(find kth smallest element in 2 sorted arrays)
查找两个有序数组中的第K个元素
int FindKth(int a[], int b[], int k, int astart, int aend, int bstart, int bend)
{
int aLen = aend - astart + ;
int bLen = bend - bstart + ; if (aLen == )
{
return b[bstart + k];
} if (bLen == )
{
return a[astart + k];
} if (k == )
{
return a[astart] > b[bstart] ? b[bstart] : a[astart] ;
} int amid = aLen * k / (aLen + bLen); //按比例,算出分界点
int bmid = k - amid - ; amid += astart;
bmid += bstart; if (a[amid] > b[bmid])
{
k -= (bmid - bstart + );
aend = amid;
bstart = bmid + ;
}
else
{
k -= (amid - astart + );
bend = bmid;
astart = amid + ;
}
return FindKth(a, b, k, astart, aend, bstart, bend);
} int main(int argc, char* argv[])
{
int a[] = {, , , , , , };
int b[] = {, , , }; printf("\nfind 0th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 1th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 2th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 3th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 4th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 5th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 6th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 7th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 8th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 9th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 10th it=%d\n", FindKth(a, b, , , , , )); getchar();
return ;
}
查找两个有序数组中的第K个元素(find kth smallest element in 2 sorted arrays)的更多相关文章
- Coursera Algorithms week3 快速排序 练习测验: Selection in two sorted arrays(从两个有序数组中寻找第K大元素)
题目原文 Selection in two sorted arrays. Given two sorted arrays a[] and b[], of sizes n1 and n2, respec ...
- [转载]寻找两个有序数组中的第K个数或者中位数
http://blog.csdn.net/realxie/article/details/8078043 假设有长度分为为M和N的两个升序数组A和B,在A和B两个数组中查找第K大的数,即将A和B按升序 ...
- 选取两个有序数组中最大的K个值,降序存入另一个数组中
原题: 假设有两个有序的整型数组int *a1, int *a2,长度分别为m和n.试用C语言写出一个函数选取两个数组中最大的K个值(K可能大于m+n)写到int *a3中,保持a3降序,并返回a3实 ...
- Leetcode 703题数据流中的第K大元素(Kth Largest Element in a Stream)Java语言求解
题目链接 https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/ 题目内容 设计一个找到数据流中第K大元素的类(class) ...
- LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13
378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
- 两个有序数组中的中位数以及求第k个最小数的值
解法参考 <[分步详解]两个有序数组中的中位数和Top K问题> https://blog.csdn.net/hk2291976/article/details/51107778 里面求中 ...
- [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 ...
- 如何寻找无序数组中的第K大元素?
如何寻找无序数组中的第K大元素? 有这样一个算法题:有一个无序数组,要求找出数组中的第K大元素.比如给定的无序数组如下所示: 如果k=6,也就是要寻找第6大的元素,很显然,数组中第一大元素是24,第二 ...
- [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 ...
随机推荐
- 关于distinct的compare参数的使用
1.创建compare类 using DCZY.Bean; using System; using System.Collections.Generic; using System.Linq; usi ...
- 前端js之BOM和DOM操作
目录 引入 BOM操作 window对象 history对象 location对象(重点) 弹出框 定时器 计时器相关 DOM 查找标签 直接查找 间接查找 节点操作 创建节点及添加节点 删除节点 替 ...
- 【JZOJ5428】【NOIP2017提高A组集训10.27】查询
题目 给出一个长度为n的序列a[] 给出q组询问,每组询问形如\(<x,y>\),求a序列的所有区间中,数字x的出现次数与数字y的出现次数相同的区间有多少个. 分析 我们可以维护一个前缀和 ...
- JZOJ5373【NOIP2017提高A组模拟9.17】信仰是为了虚无之人
题目 分析 我们发现,如果[l,r]的异或和为k是真要求,有且仅当不存在[l,i]和[i,r]两个区间的异或和不为k. 我们用带权并查集了维护这些,但是,由于区间不连续,我们将点权移到边上,对于区间[ ...
- Python 文件I/O Ⅳ
Python里的目录: 所有文件都包含在各个不同的目录下,不过Python也能轻松处理.os模块http://www.xuanhe.net/有许多方法能帮你创建,删除和更改目录. mkdir()方法 ...
- git的安装教程
团队开发的时候常要用到git来对代码进行管理,平时开发的时候也需要使用git来做一些事情,如给vue项目进行代码编译等等. 那下面我们开始 一.安装和配置环境 首先下载git安装包(去官网下载,htt ...
- BZOJ 1231: [Usaco2008 Nov]mixup2 混乱的奶牛 状态压缩dp
开始读错题了,然后发现一眼切~ Code: #include <cstdio> #include <algorithm> #define ll long long #defin ...
- datetime模块的常用总结
datetime模块 datetime模块提供了一些处理日期和时间的标准库.常用的有 datetime timedelta timezone 构造一个datetime对象 datetime() dat ...
- Springboot 使用JdbcTemplate
Springboot 使用JdbcTemplate book package com.draymonder.book.jdbc; public class Book { private Integer ...
- CSP2019-S2参赛总结 暨 近期学习反思
前言 岁月不居,时节如流.眨眼间,2019的联赛就已经落下帷幕了,回忆这一年的学习,有许许多多的事情想写下来.趁联赛结果还未出来,赶紧写下这篇文章,以记录我这段时间的学习和生活. "你怎么又 ...