 wrapper class: Point
 value
 flag: 1 indicates start, 2 indicates end
 index: original pos in intervals array
 Comparable: sort by value ascending, end in front of start if they have same value.

Iterate intervals array and fill a points list, then sort it

Iterate points list, since the sequence will be "order by position, and end will come before start".
 whenever meet a end point, keep a list(prevIdxs) before next start, save original index of curr interval to the list.
 whenever meet a start point, this start point is the right interval to the intervals in the list (prevIdxs). Take out each index in it and update to result.
class Point implements Comparable<Point>{
int val;
int flag; //1 start, 0 end
int index;
public Point(int val, int flag, int index) {
this.val = val;
this.flag = flag;
this.index = index;
}
public int compareTo(Point o) {
if (this.val == o.val) return this.flag  o.flag; //end in front of start
return this.val  o.val;
}
}
public int[] findRightInterval(Interval[] intervals) {
if (intervals == null  intervals.length == 0) return new int[]{};
int[] res = new int[intervals.length];
Arrays.fill(res, 1);
List<Point> points = new ArrayList<>();
for (int i = 0; i < intervals.length; i++) {
points.add(new Point(intervals[i].start, 1, i));
points.add(new Point(intervals[i].end, 0, i));
}
Collections.sort(points);
List<Integer> prevIdxs = new ArrayList<>();
for (Point point: points) {
if (point.flag == 1) {
for (Integer prevIdx: prevIdxs) {
res[prevIdx] = point.index;
}
prevIdxs = new ArrayList<>();
} else {
prevIdxs.add(point.index);
}
}
return res;
}