Skip to content

python code issue in cpp code #4461

@akgupta1337

Description

@akgupta1337

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy