Skip to content

Commit

Permalink
add java 435, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 13, 2024
1 parent 4b1b202 commit c2f1bfb
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@
392| [Is Subsequence](https://leetcode.com/problems/is-subsequence/)| [Python](./leetcode_python/Greedy/is-subsequence.py) | _O(n)_ | _O(1)_ | Medium |`basics`| OK*
397| [Integer Replacement](https://leetcode.com/problems/integer-replacement/)| [Python](./leetcode_python/Greedy/integer-replacement.py) | _O(n)_ | _O(1)_ | Medium |`Recursion basics`, Math | OK*
402 | [Remove K Digits](https://leetcode.com/problems/remove-k-digits/) | [Python](./leetcode_python/Greedy/remove-k-digits.py) | _O(n)_ | _O(n)_ | Medium | LintCode | AGAIN (not start*)
435 | [Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/) | [Python](./leetcode_python/Greedy/non-overlapping-intervals.py) | _O(nlogn)_ | _O(1)_ | Medium | Curated Top 75, good trick, 2 pointers, inverval, similiar as `#452 Minimum Number of Arrows to Burst Balloons`, Line Sweep, `google` | AGAIN********* (6)
435 | [Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/) | [Python](./leetcode_python/Greedy/non-overlapping-intervals.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/NonOverlappingIntervals.java) | _O(nlogn)_ | _O(1)_ | Medium | Curated Top 75, good trick, 2 pointers, inverval, similiar as `#452 Minimum Number of Arrows to Burst Balloons`, Line Sweep, `google` | AGAIN********* (7)
452 | [Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/) | [Python](./leetcode_python/Greedy/minimum-number-of-arrows-to-burst-balloons.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/MinimumNumberOfArrowsToBurstBalloons.java)| _O(nlogn)_ | _O(1)_ | Medium | sort, compare i-1 element, overlap, good basic| AGAIN***** (3)
455 | [Assign Cookies](https://leetcode.com/problems/assign-cookies/) | [Python](./leetcode_python/Greedy/assign-cookies.py) | _O(nlogn)_ | _O(1)_ | Easy | | OK*
621 | [Task Scheduler](https://leetcode.com/problems/task-scheduler/) | [Python](./leetcode_python/Greedy/task-scheduler.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/TaskScheduler.java) | _O(n)_ | _O(1)_ | Medium |`trick`,`array`,`greedy`, `fb`, `amazon` | AGAIN****** (7)
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 @@
20240313: 338,347,371,417(again),424(again)
20240313: 338,347,371,417(again),424(again),435
20240312: 297(again),300,322(again),323
20240311: 261(again!!!),268,269(again),271,295(again)
20240310: 213,217,226,230,235,238,242,252,253
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-07 -> ['338,347,371,417(again),424(again)']
2024-05-07 -> ['338,347,371,417(again),424(again),435']
2024-05-06 -> ['297(again),300,322(again),323']
2024-05-05 -> ['261(again!!!),268,269(again),271,295(again)']
2024-05-04 -> ['213,217,226,230,235,238,242,252,253']
Expand All @@ -12,7 +12,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-16 -> ['338,347,371,417(again),424(again)']
2024-04-16 -> ['338,347,371,417(again),424(again),435']
2024-04-15 -> ['297(again),300,322(again),323']
2024-04-14 -> ['261(again!!!),268,269(again),271,295(again)']
2024-04-13 -> ['213,217,226,230,235,238,242,252,253']
Expand All @@ -23,27 +23,27 @@
2024-04-08 -> ['100,102,104,105(again)']
2024-04-07 -> ['73,76,79(again),91,25']
2024-04-06 -> ['55(again),56,62,70']
2024-04-03 -> ['338,347,371,417(again),424(again)', '39,48(again),49,53,54']
2024-04-03 -> ['338,347,371,417(again),424(again),435', '39,48(again),49,53,54']
2024-04-02 -> ['297(again),300,322(again),323', '20,21,23,33(again)']
2024-04-01 -> ['261(again!!!),268,269(again),271,295(again)', '1,3,5,4,19']
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 -> ['338,347,371,417(again),424(again)', '100,102,104,105(again)']
2024-03-26 -> ['338,347,371,417(again),424(again),435', '100,102,104,105(again)']
2024-03-25 -> ['297(again),300,322(again),323', '73,76,79(again),91,25']
2024-03-24 -> ['261(again!!!),268,269(again),271,295(again)', '55(again),56,62,70']
2024-03-23 -> ['213,217,226,230,235,238,242,252,253']
2024-03-22 -> ['207(again!!!),208,211(again),212']
2024-03-21 -> ['338,347,371,417(again),424(again)', '152,153(again),190,191,198,200,206(again)', '39,48(again),49,53,54']
2024-03-21 -> ['338,347,371,417(again),424(again),435', '152,153(again),190,191,198,200,206(again)', '39,48(again),49,53,54']
2024-03-20 -> ['297(again),300,322(again),323', '139(again),141,143(again)', '20,21,23,33(again)']
2024-03-19 -> ['261(again!!!),268,269(again),271,295(again)', '121,124(again),125,128,133(again)', '1,3,5,4,19']
2024-03-18 -> ['338,347,371,417(again),424(again)', '213,217,226,230,235,238,242,252,253', '100,102,104,105(again)']
2024-03-18 -> ['338,347,371,417(again),424(again),435', '213,217,226,230,235,238,242,252,253', '100,102,104,105(again)']
2024-03-17 -> ['297(again),300,322(again),323', '207(again!!!),208,211(again),212', '73,76,79(again),91,25']
2024-03-16 -> ['338,347,371,417(again),424(again)', '261(again!!!),268,269(again),271,295(again)', '152,153(again),190,191,198,200,206(again)', '55(again),56,62,70']
2024-03-15 -> ['338,347,371,417(again),424(again)', '297(again),300,322(again),323', '213,217,226,230,235,238,242,252,253', '139(again),141,143(again)']
2024-03-14 -> ['338,347,371,417(again),424(again)', '297(again),300,322(again),323', '261(again!!!),268,269(again),271,295(again)', '207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)']
2024-03-16 -> ['338,347,371,417(again),424(again),435', '261(again!!!),268,269(again),271,295(again)', '152,153(again),190,191,198,200,206(again)', '55(again),56,62,70']
2024-03-15 -> ['338,347,371,417(again),424(again),435', '297(again),300,322(again),323', '213,217,226,230,235,238,242,252,253', '139(again),141,143(again)']
2024-03-14 -> ['338,347,371,417(again),424(again),435', '297(again),300,322(again),323', '261(again!!!),268,269(again),271,295(again)', '207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)']
2024-03-13 -> ['297(again),300,322(again),323', '261(again!!!),268,269(again),271,295(again)', '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 -> ['261(again!!!),268,269(again),271,295(again)', '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']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package LeetCodeJava.Greedy;

// https://leetcode.com/problems/non-overlapping-intervals/description/

import java.util.Arrays;
import java.util.Stack;
import java.util.Comparator;

public class NonOverlappingIntervals {

// V0
// IDEA : 2 POINTERS + sorting + intervals (modified by GPT)
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Greedy/non-overlapping-intervals.py
public int eraseOverlapIntervals(int[][] intervals) {
Arrays.sort(intervals, Comparator.comparingInt(a -> a[1]));
int cnt = 0;
int[] last = intervals[0];

for (int i = 1; i < intervals.length; i++) {
if (intervals[i][0] < last[1]) {
cnt++;
} else {
last[1] = Math.max(intervals[i][1], last[1]);
}
}
return cnt;
}


// V0'
// TODO : implement it
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Greedy/non-overlapping-intervals.py
// public int eraseOverlapIntervals(int[][] intervals) {
// return 0;
// }

// V1
// IDEA : STACK
// https://leetcode.com/problems/non-overlapping-intervals/solutions/4683650/4-line-solution-easy-to-understand-java-stack/
public int eraseOverlapIntervals_1(int[][] intervals) {
Arrays.sort(intervals,(a, b)->a[1]-b[1]);
Stack<int[]> stk = new Stack<>();
int res=0;
for(int[] i : intervals){
if(!stk.isEmpty() && i[0]< stk.peek()[1]){
res++;
}else{
stk.push(i);
}
}

return res;
}

// V2
// IDEA : SORT
// https://leetcode.com/problems/non-overlapping-intervals/solutions/3786438/java-easy-solution-using-sorting-explained/
public int eraseOverlapIntervals_2(int[][] intervals) {
// Sort by ending time
Arrays.sort(intervals, (a, b) -> Integer.compare(a[1], b[1]));
int prev = 0, count = 1;
// if end is same, sort by start, bigger start in front
for(int i = 0; i < intervals.length; i ++) {
if(intervals[i][0] >= intervals[prev][1]) {
prev = i;
count ++;
}
}
return intervals.length - count;
}

// V3
// https://leetcode.com/problems/non-overlapping-intervals/submissions/1202459919/
public int eraseOverlapIntervals_3(int[][] in) {
Arrays.sort(in, (a,b)->a[1]-b[1]);
int res=-1, p[]=in[0];
for(int[] i: in)
if(i[0]<p[1]) res++;
else p=i;
return res;
}

}

0 comments on commit c2f1bfb

Please sign in to comment.