Leetcode day1

This commit is contained in:
2026-01-25 12:41:33 +05:30
parent 686d174f19
commit 245116d181
14 changed files with 168 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int a = nums1.size();
int b = nums2.size();
int i=0,j=0,m1=0,m2=0;
for(int count=0;count<=(a+b)/2;count++){
m2=m1;
if(i!=a && j!=b){
if(nums1[i]<nums2[j]){
m1= nums1[i++];
}
else{
m1 = nums2[j++];
}
}
else if(i<a){
m1 = nums1[i++];
}
else {
m1 = nums2[j++];
}
}
if((a+b)%2==1){
return (double)m1;
}
else{
return (m1+m2)/2.0;
}
}
};
+21
View File
@@ -0,0 +1,21 @@
class Solution{
public :
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2){
int n = nums1.size();
int m = nums2.size();
vector<int> merger;
for(auto nums: nums1)
merger.push_back(nums);
for(auto nums : nums2)
merger.push_back(nums);
sort(merger.begin(), meger.end());
int size = meger.size();
if(size%2==1)
return (double) merger[size/2];
else
return (double) (merger[size/2]+ merger[size/2 - 1])/2.0;
}
};
+5
View File
@@ -0,0 +1,5 @@
The brute force approach is quite intuitive but doesnt meet the specified Run time complexity of O(log(m+n)) instead takes O((m+n)log(m+n)) taken by sorting
TwoPointer method takes O(n+m) where we only merge till half and then extract the median which is the middle element
Yet to learn binarySearch approach