Skip to content

Commit 6563240

Browse files
committed
update 253 java, progress
1 parent 30b24d6 commit 6563240

File tree

5 files changed

+223
-14
lines changed

5 files changed

+223
-14
lines changed

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-11-23
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-11-22
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20241123: 253
12
20241122: 776,31
23
20241121: 004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)
34
20241120: 986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)

data/to_review.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2025-01-17 -> ['253']
12
2025-01-16 -> ['776,31']
23
2025-01-15 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
34
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
@@ -7,31 +8,31 @@
78
2025-01-03 -> ['444']
89
2025-01-02 -> ['1188,130,855(again)']
910
2024-12-28 -> ['900']
10-
2024-12-27 -> ['26,27', '802,1197,26']
11+
2024-12-27 -> ['253', '26,27', '802,1197,26']
1112
2024-12-26 -> ['776,31']
1213
2024-12-25 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
1314
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
1415
2024-12-21 -> ['394', '855,846']
1516
2024-12-20 -> ['833,950', '932']
1617
2024-12-18 -> ['951,792']
17-
2024-12-14 -> ['53,210,207', '163,1048']
18+
2024-12-14 -> ['253', '53,210,207', '163,1048']
1819
2024-12-13 -> ['776,31', '444', '298,729']
1920
2024-12-12 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
2021
2024-12-11 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2122
2024-12-08 -> ['394', '737']
2223
2024-12-07 -> ['833,950', '900', '686,734,737']
23-
2024-12-06 -> ['26,27', '802,1197,26', '353']
24+
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']
2425
2024-12-05 -> ['776,31', '528,334']
2526
2024-12-04 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
2627
2024-12-03 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
27-
2024-12-01 -> ['53,210,207']
28+
2024-12-01 -> ['253', '53,210,207']
2829
2024-11-30 -> ['776,31', '394', '444', '855,846', '1145,1219']
2930
2024-11-29 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
30-
2024-11-28 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
31+
2024-11-28 -> ['253', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
3132
2024-11-27 -> ['776,31', '951,792', '524,221,889']
32-
2024-11-26 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '743,889']
33-
2024-11-25 -> ['776,31', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '837']
34-
2024-11-24 -> ['776,31', '004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '900']
33+
2024-11-26 -> ['253', '004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '743,889']
34+
2024-11-25 -> ['253', '776,31', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '837']
35+
2024-11-24 -> ['253', '776,31', '004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '900']
3536
2024-11-23 -> ['776,31', '004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '53,210,207', '26,27', '802,1197,26', '163,1048', '981']
3637
2024-11-22 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '444', '298,729', '1087']
3738
2024-11-21 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '833,950', '1188,130,855(again)', '1146']

leetcode_java/src/main/java/LeetCodeJava/Sort/MeetingRooms2.java

Lines changed: 116 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
package LeetCodeJava.Sort;
22

3-
import java.util.Arrays;
4-
import java.util.Comparator;
5-
import java.util.PriorityQueue;
6-
import java.util.Queue;
3+
import java.util.*;
74

85

96
// https://leetcode.com/problems/meeting-rooms-ii/description/
10-
7+
// https://leetcode.ca/all/253.html
8+
/**
9+
* 253. Meeting Rooms II
10+
* Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.
11+
*
12+
* Example 1:
13+
*
14+
* Input: [[0, 30],[5, 10],[15, 20]]
15+
* Output: 2
16+
* Example 2:
17+
*
18+
* Input: [[7,10],[2,4]]
19+
* Output: 1
20+
* NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
21+
*
22+
* Difficulty:
23+
* Medium
24+
* Lock:
25+
* Prime
26+
* Company:
27+
* Amazon Apple Atlassian Baidu Bloomberg Booking.com Cisco Citrix Drawbridge eBay Expedia Facebook GoDaddy Goldman Sachs Google Lyft Microsoft Nutanix Oracle Paypal Postmates Quora Snapchat Uber Visa Walmart Labs Yelp
28+
*/
1129

1230
public class MeetingRooms2 {
1331

@@ -40,7 +58,82 @@ public int minMeetingRooms(int[][] intervals) {
4058
}
4159

4260
// V0'
43-
// TODO : validate below
61+
// TODO : validate
62+
// IDEA : SCANNING LINE
63+
// ref code : https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Sort/meeting-rooms-ii.py#L90
64+
class Meeting {
65+
int time;
66+
String status;
67+
68+
Meeting(int time, String status) {
69+
this.time = time;
70+
this.status = status;
71+
}
72+
73+
public int getTime() {
74+
return time;
75+
}
76+
77+
public String getStatus() {
78+
return status;
79+
}
80+
}
81+
82+
public class MeetingRooms_0_1 {
83+
public int minMeetingRooms(int[][] intervals) {
84+
if (intervals.length <= 1) {
85+
return intervals.length;
86+
}
87+
88+
List<Meeting> meetings = new ArrayList<>();
89+
90+
// Convert intervals into meeting start and end events
91+
for (int[] interval : intervals) {
92+
int start = interval[0];
93+
int end = interval[1];
94+
meetings.add(new Meeting(start, "open"));
95+
meetings.add(new Meeting(end, "end"));
96+
}
97+
98+
// Sort meetings: by time ascending; if times are equal, "end" comes before "open"
99+
// V1
100+
// meetings.sort((x,y) -> {
101+
// if (x.getTime() < y.getTime()){
102+
// return -1;
103+
// }
104+
// if (x.getTime() > y.getTime()){
105+
// return 1;
106+
// }
107+
// return 0;
108+
// });
109+
110+
// V2
111+
meetings.sort((x, y) -> {
112+
if (x.getTime() != y.getTime()) {
113+
return Integer.compare(x.getTime(), y.getTime());
114+
}
115+
return x.getStatus().equals("end") ? -1 : 1;
116+
});
117+
118+
// Track room requirements
119+
int maxRooms = 0;
120+
int currentRooms = 0;
121+
122+
for (Meeting meeting : meetings) {
123+
if (meeting.getStatus().equals("open")) {
124+
currentRooms++;
125+
} else {
126+
currentRooms--;
127+
}
128+
maxRooms = Math.max(maxRooms, currentRooms);
129+
}
130+
131+
return maxRooms;
132+
}
133+
}
134+
135+
// V0'
136+
// TODO : validate
44137
// IDEA : ARRAY SORT + BOUNDARY OP
45138
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Sort/meeting-rooms-ii.py#L90
46139
// IDEA : CREATE A NEW ARRAY WITH
@@ -137,4 +230,21 @@ public int minMeetingRooms_2(int[][] intervals) {
137230
return ans;
138231
}
139232

233+
// V3
234+
// https://leetcode.ca/2016-08-09-253-Meeting-Rooms-II/
235+
public int minMeetingRooms_3(int[][] intervals) {
236+
int n = 1000010;
237+
int[] delta = new int[n];
238+
for (int[] e : intervals) {
239+
++delta[e[0]];
240+
--delta[e[1]];
241+
}
242+
int res = delta[0];
243+
for (int i = 1; i < n; ++i) {
244+
delta[i] += delta[i - 1];
245+
res = Math.max(res, delta[i]);
246+
}
247+
return res;
248+
}
249+
140250
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,6 +3549,100 @@ public void nextPermutation(int[] nums) {
35493549

35503550
}
35513551

3552+
// LC 253
3553+
// https://leetcode.ca/all/253.html
3554+
// 4.57 pm - 5.10 pm
3555+
/**
3556+
* [[s1,e1],[s2,e2],...]
3557+
*
3558+
* s : start, e : end
3559+
*
3560+
* Idea 1) sorting + scanning line
3561+
*
3562+
* -> sort by s, e
3563+
* -> get the "max" concurrent room count as res
3564+
* -> return res
3565+
*
3566+
*
3567+
* |------------------------|
3568+
* 0 30
3569+
* |-------|
3570+
* 5 10
3571+
* |-----|
3572+
* 15 20
3573+
*
3574+
*
3575+
* [0,5,10,15,20,30]
3576+
* o o c o c c
3577+
*/
3578+
3579+
class Meeting{
3580+
int time;
3581+
String status;
3582+
3583+
Meeting(){
3584+
3585+
}
3586+
3587+
Meeting(int time, String status){
3588+
this.time = time;
3589+
this.status = status;
3590+
}
3591+
3592+
public int getTime() {
3593+
return time;
3594+
}
3595+
3596+
public String getStatus() {
3597+
return status;
3598+
}
3599+
}
3600+
public int minMeetingRooms(int[][] intervals) {
3601+
if (intervals.length <= 1){
3602+
return intervals.length;
3603+
}
3604+
3605+
List<Meeting> meetings = new ArrayList<>();
3606+
3607+
for (int[] item : intervals){
3608+
//Meeting meeting = new Meeting()
3609+
int start = item[0];
3610+
int end = item[1];
3611+
3612+
Meeting meetingOpen = new Meeting(start, "open");
3613+
Meeting meetingEnd = new Meeting(end, "end");
3614+
3615+
meetings.add(meetingOpen);
3616+
meetings.add(meetingEnd);
3617+
}
3618+
3619+
// sort as ascending order, e.g. [1,4,2] -> [1,2,4]
3620+
meetings.sort((x,y) -> {
3621+
if (x.getTime() < y.getTime()){
3622+
return -1;
3623+
}
3624+
if (x.getTime() > y.getTime()){
3625+
return 1;
3626+
}
3627+
return 0;
3628+
});
3629+
3630+
3631+
int cnt = 0;
3632+
int curCnt = 0;
3633+
3634+
for (Meeting meeting : meetings){
3635+
if (meeting.getStatus().equals("open")){
3636+
curCnt += 1;
3637+
}else{
3638+
curCnt -= 1;
3639+
}
3640+
cnt = Math.max(cnt, curCnt);
3641+
}
3642+
3643+
return cnt;
3644+
}
3645+
35523646
}
35533647

35543648

0 commit comments

Comments
 (0)