After I submitted my solution to the Maximum Subarray problem on leetcode, it said my runtime was 1ms (beating ~66% submissions), then I made a couple of changes and resubmitted. This time, the runtime was 0ms (beating ~100%). I do not quite understand what contributed to the reduction in time. Here the are two versions.
Original:
public int maxSubArray(int[] nums) {
        if (nums == null) return 0;
        if (nums.length == 1) return nums[0];
        int current_max = nums[0];
        int cur = 1;
        int max = current_max;
        while (cur < nums.length) {
            current_max = Math.max(nums[cur] + current_max, nums[cur]);
            if (current_max > max) {
                max = current_max;
            }
            cur++;
        }
        return max;
    }
Updated:
public int maxSubArray(int[] nums) {
        if (nums == null) return 0;
        int l = nums.length;
        if (l == 1) return nums[0];
        int current_max = nums[0];
        int max = current_max;
        for (int i = 1; i < l; i++) {
            current_max = Math.max(nums[i] + current_max, nums[i]);
            if (current_max > max) {
                max = current_max;
            }
        }
        return max;
    }
The only two changes I did, were
- Changing the while loop to a for loop - Apparently there is no difference ref
- Store the length in a separate variable - Since Array does not compute the length and array.lengthis only fetching from an internal variable, I do not see any difference here either.
[EDIT]
I agree. leetcode's performance is not 100% accurate. Funny thing, I submitted my updated code again the other day, and suddenly now it's 0ms.
 
    