-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Open
Description
https://github.com/neetcode-gh/leetcode/blob/main/python%2F0004-median-of-two-sorted-arrays.py
this question 4 of leetcode, i asked claude to generate correspoinding cpp code for this which is this
however on leetcode this cpp code is giving TLE while python one is passing, how is this possible?? this cpp code is literally replica of that python code
// Time: O(log(min(n, m)))
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
vector<int>& A = nums1;
vector<int>& B = nums2;
int total = nums1.size() + nums2.size();
int half = total / 2;
if (B.size() < A.size()) {
swap(A, B);
}
int l = 0, r = A.size() - 1;
while (true) {
int i = (l + r) / 2; // A
int j = half - i - 2; // B
int Aleft = (i >= 0) ? A[i] : INT_MIN;
int Aright = (i + 1 < A.size()) ? A[i + 1] : INT_MAX;
int Bleft = (j >= 0) ? B[j] : INT_MIN;
int Bright = (j + 1 < B.size()) ? B[j + 1] : INT_MAX;
// partition is correct
if (Aleft <= Bright && Bleft <= Aright) {
// odd
if (total % 2) {
return min(Aright, Bright);
}
// even
return (max(Aleft, Bleft) + min(Aright, Bright)) / 2.0;
}
else if (Aleft > Bright) {
r = i - 1;
}
else {
l = i + 1;
}
}
}
};
'''' code
Metadata
Metadata
Assignees
Labels
No labels