Posts

Showing posts from August, 2024

Insert an interval between an array of intervals

In this problem statement we have to insert a given interval between a given array of intervals.  Insert Interval Leetcode Approach:- Here we Insert the given interval in the array and then we use the same approach as we used in merge interval Leetcode question   class Pair { int x ; int y ; Pair ( int x , int y ) { this . x = x; this . y = y; } } class Solution { public int [][] insert ( int [][] intervals , int [] newInterval ) { ArrayList < Pair > p = new ArrayList <>(); for ( int i = 0 ;i< intervals . length ;i++) { p . add ( new Pair (intervals[i][ 0 ], intervals[i][ 1 ])); } p . add ( new Pair (newInterval[ 0 ], newInterval[ 1 ])); Collections . sort (p, ( Pair a, Pair b) -> { return a . x - b . x ; }); Stack < Pair > st = new Stack <>(); st . push ( p . get ( 0 )); for ( int i = ...