Sunday, September 16, 2018
436. Find Right Interval
Version #1 TreeMap
Key -> start, Value -> index
20.82 %
import java.util.Optional;
class Solution {
public int[] findRightInterval(Interval[] intervals) {
if (intervals == null) {
return null;
}
TreeMap<Integer, Integer> treeMap = new TreeMap<>();
for (int i = 0; i < intervals.length; i++) {
treeMap.put(intervals[i].start, i);
}
return IntStream.range(0, intervals.length)
.map(i -> Optional.ofNullable(treeMap.ceilingEntry(intervals[i].end))
.map(entry -> entry.getValue())
.orElseGet(() -> -1)
)
.toArray();
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment