Skip to content

Commit

Permalink
add 252 java, update java-trick.md
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Jun 25, 2023
1 parent 13a50b1 commit dc67620
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [LC pattern @ blind](https://www.teamblind.com/post/New-Year-Gift---Curated-List-of-Top-100-LeetCode-Questions-to-Save-Your-Time-OaM1orEU) : Curated-List-of-Top-100-LeetCode-Questions-to-Save-Your-Time
- [Blind Curated 75](https://leetcode.com/list/xoqag3yj/)
- [LC top 100 likes](https://leetcode.com/problem-list/top-100-liked-questions/?page=1&fbclid=IwAR2MzUDDOscDklba5gL815lkLKzxK-zOR2WV2-W80_gRmvtfAsNWyut_USw)
- [neetcode 150 LC list](https://neetcode.io/practice)
- [LC Algorithm Problem Classification](https://www.programcreek.com/2013/08/leetcode-problem-classification/)
- [cheatsheet-leetcode-a4](https://cheatsheet.dennyzhang.com/cheatsheet-leetcode-a4)
- [14-patterns-to-ace-any-coding-interview-question](https://hackernoon.com/14-patterns-to-ace-any-coding-interview-question-c5bb3357f6ed)
Expand Down Expand Up @@ -709,7 +710,7 @@
147| [Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/)|[Python](./leetcode_python/Sort/insertion-sort-list.py) | _O(n^2)_ | _O(1)_ | Medium |`trick`| AGAIN* (2)
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) | _O(nlogn)_ | _O(n)_ | Easy |Curated Top 75, 🔒,`UBER`,`amazon`, `fb`| OK*** (3) (but again)
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)
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)
Expand Down
12 changes: 11 additions & 1 deletion doc/cheatsheet/java_trick.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ ans.push("a");
ans.push("b");
ans.push("c");
String.valueOf(ans);
```
```

### 1-4) Sort Array

```java
// 1) Sort integer Array
// LC 252
/// https://leetcode.com/problems/meeting-rooms/editorial/
intervals = [[0,30],[5,10],[15,20]]
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
```
58 changes: 58 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Sort/MeetingRooms.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package LeetCodeJava.Sort;

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

import java.util.Arrays;

public class MeetingRooms {

public boolean canAttendMeetings(int[][] intervals) {

// sort
//Arrays.sort(intervals, Comparator.comparingInt((x, y) -> x[0] - y[0]).reversed());
// NOTE !! we sort int array via below
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));

// check
for (int i = 0; i < intervals.length - 1; i++){
int firstStart = intervals[i][0];
int firstEnd = intervals[i][1];
int secondStart = intervals[i+1][0];
int secondEnd = intervals[i+1][1];

// below is wrong
// if ( (secondStart < firstStart && firstStart < secondEnd) || (secondStart < firstEnd && firstEnd < secondEnd) ){
// return false;
// }

// NOTE !!!
// since we already sort Array with 1st element (increasing order)
// -> so second element's start MUST BIGGER than first element
// -> so all we need to do is : comparing if first element's end time is bigger thant second element's start time
// -> e.g. if (intervals[i][1] > intervals[i+1][0])
if ( firstEnd > secondStart ){
return false;
}
}

return true;
}

// V1
// IDEA : BRUTE FORCE
// https://leetcode.com/problems/meeting-rooms/editorial/

// V1
// IDEA : SORTING
// https://leetcode.com/problems/meeting-rooms/editorial/
public boolean canAttendMeetings_2(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
for (int i = 0; i < intervals.length - 1; i++) {
if (intervals[i][1] > intervals[i + 1][0]) {
return false;
}
}
return true;
}

}

0 comments on commit dc67620

Please sign in to comment.