From c2f1bfb424f56d255fa3ca2b2b05b4764e44531d Mon Sep 17 00:00:00 2001 From: yennanliu Date: Wed, 13 Mar 2024 19:27:25 +0800 Subject: [PATCH] add java 435, update progress --- README.md | 2 +- data/progress.txt | 2 +- data/to_review.txt | 18 ++-- .../Greedy/NonOverlappingIntervals.java | 83 +++++++++++++++++++ 4 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 leetcode_java/src/main/java/LeetCodeJava/Greedy/NonOverlappingIntervals.java diff --git a/README.md b/README.md index 25d10624..6b7ece5e 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/data/progress.txt b/data/progress.txt index 0b4100dc..be494012 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -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 diff --git a/data/to_review.txt b/data/to_review.txt index c4a6be40..dbc33aee 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -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'] @@ -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'] @@ -23,7 +23,7 @@ 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'] @@ -31,19 +31,19 @@ 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'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Greedy/NonOverlappingIntervals.java b/leetcode_java/src/main/java/LeetCodeJava/Greedy/NonOverlappingIntervals.java new file mode 100644 index 00000000..0fe78757 --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/Greedy/NonOverlappingIntervals.java @@ -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 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]