当前位置:网站首页>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));
}
}
};
边栏推荐
- [error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)
- Vscode设置标签页多行显示
- Data security industry series Salon (III) | data security industry standard system construction theme Salon
- 618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
- Sqlserver queries which indexes are underutilized
- 机器学习-感知机模型
- TCP server communication process (important)
- SSM整合-异常处理器及项目异常处理方案
- 忆当年高考|成为程序员的你,后悔了吗?
- Maui learning road (III) -- in depth discussion of winui3
猜你喜欢

Download blender on Alibaba cloud image station
![[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)](/img/3f/79dcfcd88d779a5d493b4b539bd448.jpg)
[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)

mysql数据库mysqldump为啥没有创建数据库的语句

中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
![[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)](/img/c7/1949894e106036d2b412bcd6f70245.jpg)
[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)

Vscade set multi line display of tab

Summary of multithreading and thread synchronization knowledge

IDEA中设置背景图片(超详细)

头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》

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...
随机推荐
Which software is good for machine vision?
False summer vacation
图书管理系统(山东农业大学课程设计)
By asp Net core downloads files according to the path exception
Take you ten days to easily complete the go micro service series (I)
Some problems about MySQL installation
Remove the underline in router link
云原生的 CICD 框架:Tekton
Memory alignment of structure
Yyds dry inventory method of deleting expired documents in batch
2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time
Multi task prompt learning: how to train a large language model?
数据安全产业系列沙龙(三)| 数据安全产业标准体系建设主题沙龙
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条...
Aike AI frontier promotion (2.15)
ROW_NUMBER()、RANK()、DENSE_RANK区别
(practice C language every day) the sum of the nearest three numbers
Original God 2.6 server download and installation tutorial
Pandora IOT development board learning (RT thread) - Experiment 2 RGB LED experiment (learning notes)
ROW_ NUMBER()、RANK()、DENSE_ Rank difference