A leetcode problem of merging intervals

The code is as below

class Solution {
	public int[][] merge(int[][] intervals) {
		List <int[]> result = new ArrayList<> ();

        Arrays.sort(intervals, (i1, i2) -> Integer.compare(i1[0], i2[0]));
        int[] newInterval = intervals[0];
        result.add(newInterval);

        for (int[] interval: intervals) {
            if (newInterval[1] >= interval[0]){
                newInterval[1] = Math.max(newInterval[1], interval[1]);
            } else {
                newInterval = interval;
                result.add(newInterval);
            }
        }
        return result.toArray(new int[result.size()][]);

	}
}

The output for the test case [[1,8],[2,14],[3,20]] is displayed out to be [[1,20]]

I wanted to know how since the output when I go through the code manually turns out to be [[1,8]]

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.