Can someone tell me what's wrong with my solution?Getting stackoverflow


  • 0
    A

    public class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {

        return findMedian(nums1, nums2, 0, nums1.length-1, 0, nums2.length-1);
        
    }
    
    public double findMedian(int []a, int []b, int start_a, int end_a, int start_b, int end_b){
        
        if (start_a> end_a || start_b> end_b) return 0;
        if (a.length ==0) return (b[((start_b+end_b)/2)]);
        if (b.length ==0) return (a[((start_a+end_a)/2)]);
        // base case n==2
        if (end_a-start_a ==1 && end_b-start_b ==1) {
            double sum = Math.max(a[start_a], b[start_b]) + Math.min(a[end_a], b[end_b]);
            return 1.0* (sum/2);
        }
        
        int m1_ind = (start_a + end_a)/2;
        int m2_ind = (start_b + end_b)/2;
        int m1 = a[m1_ind];
        int m2 = b[m2_ind];
        
        if (m1 == m2) return m1;
        
        if (m1< m2){
            start_a = m1_ind;
            end_b = m2_ind;
        }
        
        if (m2<m1){
            start_b = m2_ind;
            end_a = m1_ind;
        }
        
        return findMedian(a,b, start_a, end_a, start_b, end_b);
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.