Skip to content

Commit 6707b83

Browse files
add 1024
1 parent e38f140 commit 6707b83

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ _If you like this project, please leave me a star._ ★
390390
|1030|[Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1030.java) | |Easy|
391391
|1029|[Two City Scheduling](https://leetcode.com/problems/two-city-scheduling/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1029.java) | |Easy|
392392
|1025|[Divisor Game](https://leetcode.com/problems/divisor-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1025.java) | |Easy|Math, DP, Brainteaser, Game Theory|
393+
|1024|[Video Stitching](https://leetcode.com/problems/video-stitching/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1024.java) | |Medium|Array, DP, Greedy|
393394
|1022|[Sum of Root To Leaf Binary Numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1022.java) | |Easy|
394395
|1021|[Remove Outermost Parentheses](https://leetcode.com/problems/remove-outermost-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1021.java) | |Easy|
395396
|1020|[Number of Enclaves](https://leetcode.com/problems/number-of-enclaves/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1020.java) | |Medium|Graph, DFS, BFS, recursion|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
public class _1024 {
6+
public static class Solution1 {
7+
/**
8+
* Greedy
9+
* Time: O(nlogn) where n is the number of clips
10+
* Space: O(1)
11+
*/
12+
public int videoStitching(int[][] clips, int time) {
13+
Arrays.sort(clips, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
14+
int count = 0;
15+
int covered = 0;
16+
for (int i = 0, start = 0; start < time; count++, start = covered) {
17+
for (; i < clips.length && clips[i][0] <= start; i++) {
18+
covered = Math.max(covered, clips[i][1]);
19+
}
20+
if (start == covered) {
21+
return -1;
22+
}
23+
}
24+
return count;
25+
}
26+
}
27+
28+
public static class Solution2 {
29+
/**
30+
* DP
31+
* Time: ?
32+
* Space: ?
33+
*/
34+
public int videoStitching(int[][] clips, int time) {
35+
//TODO: implement it.
36+
return -1;
37+
}
38+
}
39+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1024;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _1024Test {
11+
private static _1024.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1024.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(3, solution1.videoStitching(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]"), 10));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertEquals(-1, solution1.videoStitching(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,1],[1,2]"), 5));
26+
}
27+
28+
@Test
29+
public void test3() {
30+
assertEquals(-1, solution1.videoStitching(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,2],[4,8]"), 5));
31+
}
32+
33+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy