Skip to content

Commit

Permalink
Add another approach fot meeting rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
miladra committed Jul 7, 2022
1 parent 27b55ed commit baf691d
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions java/253-Meeting-Rooms-ii.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,42 @@ public int minMeetingRooms(List<Interval> intervals) {
return count;
}
}

// Two pointer approach
public class Solution {
public int minMeetingRooms(List<Interval> intervals) {
if (intervals.size() == 0) {
return 0;
}

int len = intervals.size();
int[] startTime = new int[len];
int[] endTime = new int[len];

for (int i = 0; i < len; i++) {
startTime[i] = intervals.get(i).start;
endTime[i] = intervals.get(i).end;
}

Arrays.sort(startTime);
Arrays.sort(endTime);

int res = 0;
int count = 0;
int s = 0;
int e = 0;

while (s < len) {
if (startTime[s] < endTime[e]) {
s++;
count++;
} else {
e++;
count--;
}
res = Math.max(res, count);
}

return res;
}
}

0 comments on commit baf691d

Please sign in to comment.