当前位置:网站首页>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));
}
}
};
边栏推荐
- MySQL min() finds the minimum value under certain conditions, and there are multiple results
- 关于mysql安装的一些问题
- What is normal distribution? What is the 28 law?
- Trigger: MySQL implements adding or deleting a piece of data in one table and adding another table at the same time
- LeetCode 5. 最长回文子串
- Set the background picture in the idea (ultra detailed)
- Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
- 电脑管理员权限在哪里可以打开
- JS learning notes - data types
- According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
猜你喜欢
What is normal distribution? What is the 28 law?
2022最新最详细必成功的在Vscode中设置背景图、同时解决不受支持的问题
JS learning notes - variables
Some problems about MySQL installation
Everyone Xinfu builds: a one-stop intelligent business credit service platform
Bib | graph representation based on heterogeneous information network learning to predict drug disease association
Today in history: Alipay launched barcode payment; The father of time-sharing system was born; The first TV advertisement in the world
The light of ideal never dies
理想之光不灭
Kubernetes family container housekeeper pod online Q & A?
随机推荐
AcWing 300. Task arrangement
电脑管理员权限在哪里可以打开
Memory alignment of structure
2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time
A week of short video platform 30W exposure, small magic push helps physical businesses turn losses into profits
IDEA中设置背景图片(超详细)
Trigger: MySQL implements adding or deleting a piece of data in one table and adding another table at the same time
Win11应用商店无法加载页面怎么办?Win11商店无法加载页面
LeetCode 2. 两数相加
sim2real环境配置教程
HMS core machine learning service helps zaful users to shop conveniently
LeetCode 5. 最长回文子串
JS learning notes - operators
LeetCode 4. 寻找两个正序数组的中位数(hard)
JS learning notes - first acquaintance
电脑设备打印机驱动安装失败如何解决
Student course selection system (curriculum design of Shandong Agricultural University)
Ranger (I) preliminary perception
Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
Leetcode -- number of palindromes