Skip to content

Commit

Permalink
add 253 java, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 10, 2024
1 parent b0a74ae commit 0070bd0
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@
148| [Sort List](https://leetcode.com/problems/sort-list/) | [Python](./leetcode_python/Sort/sort-list.py) | _O(nlogn)_ | _O(logn)_ | Medium |`basic`, check `# 21 Merge Two Sorted Lists`| AGAIN** (2)
179| [Largest Number](https://leetcode.com/problems/largest-number/) | [Python](./leetcode_python/Sort/largest-number.py) | _O(nlogn)_ | _O(1)_ | Medium|good basic, sort with lambda,`amazon`| AGAIN************ (3)
252| [Meeting Rooms](https://leetcode.com/problems/meeting-rooms/) | [Python](./leetcode_python/Sort/meeting-rooms.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Sort/MeetingRooms.java) | _O(nlogn)_ | _O(n)_ | Easy |Curated Top 75, sort, 🔒,`UBER`,`amazon`, `fb`| OK*** (4) (but again)
253| [Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/) | [Python](./leetcode_python/Sort/meeting-rooms-ii.py)| _O(nlogn)_ | _O(n)_ | Medium |Curated Top 75, 🔒, sort, priority queue (min-heap),`scanning line`, `trick`,`booking.com`,`good`,`UBER`, `amazon`,`google`,`fb`| AGAIN************ (5) (MUST)
253| [Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/) | [Python](./leetcode_python/Sort/meeting-rooms-ii.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Sort/MeetingRooms2.java)| _O(nlogn)_ | _O(n)_ | Medium |Curated Top 75, 🔒, sort, priority queue (min-heap),`scanning line`, `trick`,`booking.com`,`good`,`UBER`, `amazon`,`google`,`fb`| AGAIN************ (5) (MUST)
274| [H-Index](https://leetcode.com/problems/h-index/) | [Python](./leetcode_python/Sort/h-index.py) | _O(n)_ | _O(n)_ | Medium |Counting Sort,`good trick`, `fb`| AGAIN***** (3)
280| [Wiggle Sort](https://leetcode.com/problems/wiggle-sort/) | [Python](./leetcode_python/Sort/wiggle-sort.py) | _O(n)_ | _O(1)_| Medium |🔒,`google`,`fb`| OK*** (3)
324| [Wiggle Sort II](https://leetcode.com/problems/wiggle-sort-ii/) | [Python](./leetcode_python/Sort/wiggle-sort-ii.py) | _O(n)_ on average | _O(1)_ | Medium | variant of [Sort Colors](https://leetcode.com/problems/sort-colors/) , Tri Partition, `google`| AGAIN** (2)
Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240310: 213,217,226,230,235,238,242,252
20240310: 213,217,226,230,235,238,242,252,253
20240309: 207(again!!!),208,211(again),212
20240308: 152,153(again),190,191,198,200,206(again)
20240307: 139(again),141,143(again)
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2024-05-04 -> ['213,217,226,230,235,238,242,252']
2024-05-04 -> ['213,217,226,230,235,238,242,252,253']
2024-05-03 -> ['207(again!!!),208,211(again),212']
2024-05-02 -> ['152,153(again),190,191,198,200,206(again)']
2024-05-01 -> ['139(again),141,143(again)']
Expand All @@ -9,7 +9,7 @@
2024-04-24 -> ['39,48(again),49,53,54']
2024-04-23 -> ['20,21,23,33(again)']
2024-04-22 -> ['1,3,5,4,19']
2024-04-13 -> ['213,217,226,230,235,238,242,252']
2024-04-13 -> ['213,217,226,230,235,238,242,252,253']
2024-04-12 -> ['207(again!!!),208,211(again),212']
2024-04-11 -> ['152,153(again),190,191,198,200,206(again)']
2024-04-10 -> ['139(again),141,143(again)']
Expand All @@ -20,27 +20,27 @@
2024-04-03 -> ['39,48(again),49,53,54']
2024-04-02 -> ['20,21,23,33(again)']
2024-04-01 -> ['1,3,5,4,19']
2024-03-31 -> ['213,217,226,230,235,238,242,252']
2024-03-31 -> ['213,217,226,230,235,238,242,252,253']
2024-03-30 -> ['207(again!!!),208,211(again),212']
2024-03-29 -> ['152,153(again),190,191,198,200,206(again)']
2024-03-28 -> ['139(again),141,143(again)']
2024-03-27 -> ['121,124(again),125,128,133(again)']
2024-03-26 -> ['100,102,104,105(again)']
2024-03-25 -> ['73,76,79(again),91,25']
2024-03-24 -> ['55(again),56,62,70']
2024-03-23 -> ['213,217,226,230,235,238,242,252']
2024-03-23 -> ['213,217,226,230,235,238,242,252,253']
2024-03-22 -> ['207(again!!!),208,211(again),212']
2024-03-21 -> ['152,153(again),190,191,198,200,206(again)', '39,48(again),49,53,54']
2024-03-20 -> ['139(again),141,143(again)', '20,21,23,33(again)']
2024-03-19 -> ['121,124(again),125,128,133(again)', '1,3,5,4,19']
2024-03-18 -> ['213,217,226,230,235,238,242,252', '100,102,104,105(again)']
2024-03-18 -> ['213,217,226,230,235,238,242,252,253', '100,102,104,105(again)']
2024-03-17 -> ['207(again!!!),208,211(again),212', '73,76,79(again),91,25']
2024-03-16 -> ['152,153(again),190,191,198,200,206(again)', '55(again),56,62,70']
2024-03-15 -> ['213,217,226,230,235,238,242,252', '139(again),141,143(again)']
2024-03-15 -> ['213,217,226,230,235,238,242,252,253', '139(again),141,143(again)']
2024-03-14 -> ['207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)']
2024-03-13 -> ['213,217,226,230,235,238,242,252', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54']
2024-03-12 -> ['213,217,226,230,235,238,242,252', '207(again!!!),208,211(again),212', '139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-11 -> ['213,217,226,230,235,238,242,252', '207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19']
2024-03-13 -> ['213,217,226,230,235,238,242,252,253', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54']
2024-03-12 -> ['213,217,226,230,235,238,242,252,253', '207(again!!!),208,211(again),212', '139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-11 -> ['213,217,226,230,235,238,242,252,253', '207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19']
2024-03-10 -> ['207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '139(again),141,143(again)', '100,102,104,105(again)']
2024-03-09 -> ['152,153(again),190,191,198,200,206(again)', '139(again),141,143(again)', '121,124(again),125,128,133(again)', '73,76,79(again),91,25']
2024-03-08 -> ['139(again),141,143(again)', '121,124(again),125,128,133(again)', '100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54']
Expand Down
138 changes: 138 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Sort/MeetingRooms2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package LeetCodeJava.Sort;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;


// https://leetcode.com/problems/meeting-rooms-ii/description/


public class MeetingRooms2 {

// V0
// IDEA : ARRAY SORT + BOUNDARY OP
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Sort/meeting-rooms-ii.py#L90
public int minMeetingRooms(int[][] intervals) {

final int n = intervals.length;
int ans = 0;
int[] starts = new int[n];
int[] ends = new int[n];

for (int i = 0; i < n; ++i) {
starts[i] = intervals[i][0];
ends[i] = intervals[i][1];
}

Arrays.sort(starts);
Arrays.sort(ends);

// J points to ends
for (int i = 0, j = 0; i < n; ++i)
if (starts[i] < ends[j])
++ans;
else
++j;

return ans;
}

// V0'
// TODO : validate below
// IDEA : ARRAY SORT + BOUNDARY OP
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Sort/meeting-rooms-ii.py#L90
// IDEA : CREATE A NEW ARRAY WITH
// (intervals[i][0], 1) // if start point
// (intervals[i][1], -1) // if end point
// public int minMeetingRooms(int[][] intervals) {
//
// if (intervals == null || intervals.length == 0) {
// return 0;
// }
//
// // Convert intervals to a list of start and end points
// int n = intervals.length;
// int[][] points = new int[n * 2][2];
// for (int i = 0; i < n; i++) {
// points[i * 2] = new int[]{intervals[i][0], 1}; // start point
// points[i * 2 + 1] = new int[]{intervals[i][1], -1}; // end point
// }
//
// // Sort the points based on their time, if time is same then process end point first
// Arrays.sort(points, new Comparator<int[]>() {
// public int compare(int[] a, int[] b) {
// if (a[0] != b[0]) {
// return a[0] - b[0];
// } else {
// return a[1] - b[1];
// }
// }
// });
//
// // Use a min heap to track the end times of ongoing meetings
// PriorityQueue<Integer> minHeap = new PriorityQueue<>();
// int rooms = 0;
// int maxRooms = 0;
//
// for (int[] point : points) {
// if (point[1] == 1) { // start point
// rooms++;
// } else { // end point
// rooms--;
// }
// maxRooms = Math.max(maxRooms, rooms);
// }
//
// return maxRooms;
// }

// V1
// IDEA : HEAP (PQ) (priority queue)
// https://walkccc.me/LeetCode/problems/253/#__tabbed_1_2
public int minMeetingRooms_1(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> (a[0] - b[0]));

// Store the end times of each room.
Queue<Integer> minHeap = new PriorityQueue<>();

for (int[] interval : intervals) {
// There's no overlap, so we can reuse the same room.
if (!minHeap.isEmpty() && interval[0] >= minHeap.peek())
minHeap.poll();
minHeap.offer(interval[1]);
}

return minHeap.size();
}

// V2
// IDEA : SORT
// https://walkccc.me/LeetCode/problems/253/#__tabbed_1_2
public int minMeetingRooms_2(int[][] intervals) {

final int n = intervals.length;
int ans = 0;
int[] starts = new int[n];
int[] ends = new int[n];

for (int i = 0; i < n; ++i) {
starts[i] = intervals[i][0];
ends[i] = intervals[i][1];
}

Arrays.sort(starts);
Arrays.sort(ends);

// J points to ends
for (int i = 0, j = 0; i < n; ++i)
if (starts[i] < ends[j])
++ans;
else
++j;

return ans;
}

}

0 comments on commit 0070bd0

Please sign in to comment.