Issues about TLE


  • 0
    W

    Why the first script TLE and the second is not?

    The first:

    public class Solution {
        int ans = 0;
        public int reversePairs(int[] nums) {
            help(nums,0,nums.length-1);
            return ans;
        }
        public void help(int[] nums, int s, int e){
            if(s>=e) return;
            int mid = s + (e-s)/2;
            help(nums,s,mid);help(nums,mid+1,e);
            for(int i = s; i <= mid; i++){
                int j = mid+1; ///////////////////The difference here
                while(j<=e&&nums[i]/2.0>nums[j]) j++;
                ans +=j-(mid+1);
            }
            Arrays.sort(nums,s,e+1);
        }
    }
    

    The second:

    public class Solution {
        int ans = 0;
        public int reversePairs(int[] nums) {
            help(nums,0,nums.length-1);
            return ans;
        }
        public void help(int[] nums, int s, int e){
            if(s>=e) return;
            int mid = s + (e-s)/2;
            help(nums,s,mid);help(nums,mid+1,e);
            for(int i = s, j = mid+1; i <= mid; i++){
    
                while(j<=e&&nums[i]/2.0>nums[j]) j++;
                ans +=j-(mid+1);
            }
            Arrays.sort(nums,s,e+1);
        }
    }
    

    It seems that only the place where to define j changes and the time differs 30 times to the other.
    Does anyone know why?


Log in to reply
 

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