当前位置:网站首页>LeetCode 4. Find the median (hard) of two positive arrays
LeetCode 4. Find the median (hard) of two positive arrays
2022-07-02 16:40:00 【_ Liu Xiaoyu】
Given two sizes, they are m and n Positive order of ( From small to large ) Array nums1 and nums2. Please find and return the values of these two positive ordered arrays Median .
The time complexity of the algorithm should be O(log (m+n)) .
Example 1:
Input :nums1 = [1,3], nums2 = [2]
Output :2.00000
explain : Merge array = [1,2,3] , Median 2
Example 2:
Input :nums1 = [1,2], nums2 = [3,4]
Output :2.50000
explain : Merge array = [1,2,3,4] , Median (2 + 3) / 2 = 2.5
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
// Use recursion to handle
int total = nums1.size() + nums2.size();
if(total % 2 == 0)
{
int left = findKnum(nums1, 0, nums2, 0, total/2);
int right = findKnum(nums1, 0, nums2,0, total/2 + 1);
return (left + right) / 2.0;
}
else
return findKnum(nums1, 0, nums2, 0, total/2 + 1);
}
int findKnum(vector<int>& nums1, int i, vector<int>& nums2, int j, int k)
{
// nums1 A long , Put the short one in front
if(nums1.size() - i > nums2.size() - j)
return findKnum(nums2, j, nums1, i, k);
// The left array is empty
if(nums1.size() == i) return nums2[j + k - 1];
// k == 1 Directly return the minimum value of two arrays
if(k == 1) return min(nums1[i], nums2[j]);
int si = min(i + k /2, int(nums1.size())), sj = j + k /2;
if(nums1[si - 1] > nums2[sj - 1])
{
return findKnum(nums1, i, nums2, j+ k /2, k - k /2);
}
else
{
return findKnum(nums1, si, nums2,j, k - (si - i));
}
}
};
边栏推荐
- 618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
- Yyds dry goods inventory # look up at the sky | talk about the way and principle of capturing packets on the mobile terminal and how to prevent mitm
- Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share
- Download blender on Alibaba cloud image station
- Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
- Summary of monthly report | list of major events of moonbeam in June
- Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share
- Written by unity Jason
- Everyone Xinfu builds: a one-stop intelligent business credit service platform
- Where can I open computer administrator permissions
猜你喜欢

Summary | three coordinate systems in machine vision and their relationships

Pandora IOT development board learning (RT thread) - Experiment 2 RGB LED experiment (learning notes)

Practice of traffic recording and playback in vivo

sim2real环境配置教程

Mobile web development learning notes - Layout

Maui learning road (III) -- in depth discussion of winui3

Kubernetes family container housekeeper pod online Q & A?

Classifier visual interpretation stylex: Google, MIT, etc. have found the key attributes that affect image classification

Take you ten days to easily complete the go micro service series (I)

Sim2real environment configuration tutorial
随机推荐
The login box of unity hub becomes too narrow to log in
Yyds dry inventory KVM new inventory to expand space for home
Effectively use keywords to increase Amazon sales
Aike AI frontier promotion (2.15)
Student course selection system (curriculum design of Shandong Agricultural University)
[fluent] dart data type boolean type (boolean type definition | logical operation)
Routing mode: hash and history mode
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
TCP拥塞控制详解 | 2. 背景
What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?
Yyds dry inventory executor package (parameter processing function)
Vscode设置标签页多行显示
mysql min() 求某条件下最小的值出现多个结果
Where can I open computer administrator permissions
IDEA中设置背景图片(超详细)
PCL 最小中值平方法拟合平面
Library management system (Shandong Agricultural University Curriculum Design)
电脑设备打印机驱动安装失败如何解决
理想之光不灭
触发器:Mysql实现一张表添加或删除一条数据,另一张表同时添加