File tree Expand file tree Collapse file tree 5 files changed +153
-0
lines changed
non-overlapping-intervals Expand file tree Collapse file tree 5 files changed +153
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Time Complexity: O(n log n)
2
+ // Space Complexity: O(n)
3
+
4
+ var insert = function ( intervals , newInterval ) {
5
+ // append the newInterval to the intervals array
6
+ intervals . push ( newInterval ) ;
7
+
8
+ // sort the intervals array by start time
9
+ intervals . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
10
+
11
+ // to store merged intervals
12
+ let result = [ ] ;
13
+
14
+ // iterate through the sorted intervals array
15
+ for ( let i = 0 ; i < intervals . length ; i ++ ) {
16
+ // if the result array is empty or the current interval does not overlap with the last interval
17
+ if ( result . length === 0 || result [ result . length - 1 ] [ 1 ] < intervals [ i ] [ 0 ] ) {
18
+ result . push ( intervals [ i ] ) ; // Add the current interval to the result array
19
+ } else {
20
+ // if there is an overlap, merge the current interval with the last interval
21
+ result [ result . length - 1 ] [ 1 ] = Math . max (
22
+ result [ result . length - 1 ] [ 1 ] ,
23
+ intervals [ i ] [ 1 ]
24
+ ) ;
25
+ }
26
+ }
27
+
28
+ // return the final merged intervals
29
+ return result ;
30
+ } ;
Original file line number Diff line number Diff line change
1
+ // Time Complexity: O(n log n)
2
+ // Space Complexity: O(n)
3
+
4
+ export class Solution {
5
+ minMeetingRooms ( intervals ) {
6
+ // separate start and end times into different arrays
7
+ let startTimes = intervals . map ( ( interval ) => interval [ 0 ] ) ;
8
+ let endTimes = intervals . map ( ( interval ) => interval [ 1 ] ) ;
9
+
10
+ // sort the start and end times
11
+ startTimes . sort ( ( a , b ) => a - b ) ;
12
+ endTimes . sort ( ( a , b ) => a - b ) ;
13
+
14
+ let startPointer = 0 ;
15
+ let endPointer = 0 ;
16
+ let rooms = 0 ;
17
+ let maxRooms = 0 ;
18
+
19
+ // iterate over all the start times
20
+ while ( startPointer < intervals . length ) {
21
+ // if the start time is less than the end time, a new room is needed
22
+ if ( startTimes [ startPointer ] < endTimes [ endPointer ] ) {
23
+ rooms ++ ;
24
+ startPointer ++ ;
25
+ } else {
26
+ // if the start time is not less than the end time, free up a room
27
+ rooms -- ;
28
+ endPointer ++ ;
29
+ }
30
+
31
+ // update the maximum number of rooms needed
32
+ maxRooms = Math . max ( maxRooms , rooms ) ;
33
+ }
34
+
35
+ // return the maximum number of rooms needed
36
+ return maxRooms ;
37
+ }
38
+ }
Original file line number Diff line number Diff line change
1
+ // Time Complexity: O(n log n)
2
+ // Space Complexity: O(n)
3
+
4
+ var merge = function ( intervals ) {
5
+ // sort the intervals by their start time
6
+ intervals . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
7
+
8
+ // to manage merged intervals
9
+ let stack = [ ] ;
10
+
11
+ // push the first interval onto the stack
12
+ stack . push ( intervals [ 0 ] ) ;
13
+
14
+ // iterate through the sorted intervals
15
+ for ( let i = 1 ; i < intervals . length ; i ++ ) {
16
+ // get the top interval from the stack
17
+ let top = stack [ stack . length - 1 ] ;
18
+
19
+ // if the current interval overlaps with the top interval
20
+ if ( top [ 1 ] >= intervals [ i ] [ 0 ] ) {
21
+ // merge the intervals by updating the end of the top interval
22
+ top [ 1 ] = Math . max ( top [ 1 ] , intervals [ i ] [ 1 ] ) ;
23
+ } else {
24
+ // if there is no overlap, push the current interval onto the stack
25
+ stack . push ( intervals [ i ] ) ;
26
+ }
27
+ }
28
+
29
+ // return the final merged intervals
30
+ return stack ;
31
+ } ;
Original file line number Diff line number Diff line change
1
+ // Time Complexity: O(n log n)
2
+ // Space Complexity: O(1)
3
+
4
+ var eraseOverlapIntervals = function ( intervals ) {
5
+ // sort the intervals based on their end times
6
+ intervals . sort ( ( a , b ) => a [ 1 ] - b [ 1 ] ) ;
7
+
8
+ let end = intervals [ 0 ] [ 1 ] ;
9
+ let count = 0 ;
10
+
11
+ // iterate through the sorted intervals
12
+ for ( let i = 1 ; i < intervals . length ; i ++ ) {
13
+ if ( intervals [ i ] [ 0 ] < end ) {
14
+ // overlapping interval found, increment the count
15
+ count ++ ;
16
+ } else {
17
+ // no overlap, update the end to the current interval's end
18
+ end = intervals [ i ] [ 1 ] ;
19
+ }
20
+ }
21
+
22
+ return count ;
23
+ } ;
Original file line number Diff line number Diff line change
1
+ // Time Complexity: O(n^2)
2
+ // Space Complexity: O(1)
3
+
4
+ var rotate = function ( matrix ) {
5
+ const n = matrix . length ;
6
+
7
+ // perform the rotation layer by layer
8
+ for ( let layer = 0 ; layer < Math . floor ( n / 2 ) ; layer ++ ) {
9
+ let first = layer ;
10
+ let last = n - 1 - layer ;
11
+
12
+ for ( let i = first ; i < last ; i ++ ) {
13
+ let offset = i - first ;
14
+
15
+ // save the top element
16
+ let top = matrix [ first ] [ i ] ;
17
+
18
+ // move left element to top
19
+ matrix [ first ] [ i ] = matrix [ last - offset ] [ first ] ;
20
+
21
+ // move bottom element to left
22
+ matrix [ last - offset ] [ first ] = matrix [ last ] [ last - offset ] ;
23
+
24
+ // move right element to bottom
25
+ matrix [ last ] [ last - offset ] = matrix [ i ] [ last ] ;
26
+
27
+ // assign saved top element to right
28
+ matrix [ i ] [ last ] = top ;
29
+ }
30
+ }
31
+ } ;
You can’t perform that action at this time.
0 commit comments