当前位置:网站首页>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));
}
}
};
边栏推荐
- Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
- By asp Net core downloads files according to the path exception
- Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
- LeetCode 5. 最长回文子串
- sql解决连续登录问题变形-节假日过滤
- Route service grid traffic through two-level gateway design
- LeetCode 6. Z 字形变换 (N字形变换)
- LeetCode 4. 寻找两个正序数组的中位数(hard)
- JS learning notes - first acquaintance
- 关于mysql安装的一些问题
猜你喜欢
2022最新最详细必成功的在Vscode中设置背景图、同时解决不受支持的问题
unity Hub 登录框变得很窄 无法登录
Ranger (I) preliminary perception
Vscode设置标签页多行显示
The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 releases | geek headlines
Recalling the college entrance examination and becoming a programmer, do you regret it?
一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
电脑管理员权限在哪里可以打开
Aujourd'hui dans l'histoire: Alipay lance le paiement par code à barres; La naissance du père du système de partage du temps; La première publicité télévisée au monde...
OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation
随机推荐
Where can I open computer administrator permissions
Sqlserver queries which indexes are underutilized
How to choose the right kubernetes storage plug-in? (09)
AWS virtual machine expansion
Global and Chinese markets for disposable insulin pumps 2022-2028: Research Report on technology, participants, trends, market size and share
TCP server communication process (important)
LeetCode 5. 最长回文子串
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
⌈ 2022 ⌋ how to use webp gracefully in projects
What if the win11 app store cannot load the page? Win11 store cannot load page
Understand the key technology of AGV -- the difference between laser slam and visual slam
618 deep resumption: Haier Zhijia's winning methodology
机器学习-感知机模型
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
618深度複盤:海爾智家的制勝方法論
TCP拥塞控制详解 | 2. 背景
618深度复盘:海尔智家的制胜方法论
TypeScript数组乱序输出
What is Amazon keyword index? The consequences of not indexing are serious
Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function