Skip to content

Commit b90c9de

Browse files
committed
Add 1288
1 parent fe5c405 commit b90c9de

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

Readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
| 1094 | Car Pooling | Medium | O(N) | O(1) | Car Pooling | | |
121121
| 1232 | Check If It Is a Straight Line | Easy | O(N) | O(1) | Math, Pythonic, Geometry | | |
122122
| 1277 | Count Square Submatrices with All Ones | Medium | O(N * M) | O(1) | Array, Dynamic Programming | | |
123+
| 1288 | Remove Covered Intervals | Medium | O(N) | O(1) | Greedy, Sort, Line Sweep | | |
123124
| 1291 | Sequential Digits | Medium | O(N) | O(N) | Backtracking | | |
124125
| 1305 | All Elements in Two Binary Search Trees | Medium | O(N) | O(N) | Sort, Tree | | |
125126
| 1344 | Angle Between Hands of a Clock | Medium | O(1) | O(1) | Math | | |

inputs/1288

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[[1,4],[3,6],[2,8]]
2+
[[1,4],[2,3]]
3+
[[0,10],[5,12]]
4+
[[3,10],[4,10],[5,11]]
5+
[[1,2],[1,4],[3,4]]
6+
[[92, 183], [100, 924], [157, 330], [350, 602], [172, 667], [267, 757], [570, 772], [347, 439], [431, 810], [515, 901], [4, 407], [458, 520], [111, 996], [399, 965], [12, 489], [554, 716], [224, 275], [412, 614], [329, 487], [54, 861], [27, 972], [489, 857], [411, 509], [353, 600], [428, 491], [402, 559], [570, 952], [211, 798], [269, 846], [415, 433], [292, 577], [302, 880], [139, 315], [112, 755], [643, 790], [484, 734], [58, 71], [152, 954], [173, 289], [980, 987], [8, 693], [355, 684], [328, 769], [507, 723], [349, 488], [261, 821], [628, 943], [827, 840], [201, 224], [681, 908], [100, 863], [425, 549], [134, 213], [229, 656], [595, 979], [423, 870], [406, 603], [227, 491], [237, 390], [33, 151], [113, 477], [146, 610], [359, 654], [372, 777], [32, 875], [614, 708], [129, 612], [8, 619], [341, 749], [607, 897], [515, 556], [449, 904], [348, 532], [166, 751], [377, 691], [271, 686], [693, 718], [680, 818], [666, 889], [137, 975], [222, 679], [353, 924], [156, 754], [24, 618], [195, 597], [276, 776], [323, 919], [133, 496], [29, 341], [822, 906], [648, 726], [391, 546], [121, 242], [97, 949], [493, 535], [320, 444], [279, 420], [62, 878], [541, 963], [308, 738]]

src/1288.remove-covered-intervals.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#
2+
# @lc app=leetcode id=1288 lang=python3
3+
#
4+
# [1288] Remove Covered Intervals
5+
#
6+
7+
# @lc code=start
8+
# TAGS: Greedy, Sort, Line Sweep
9+
# REVIEWME: Greedy
10+
class Solution:
11+
# 108 ms, 41.65%. Brute Force: Time: O(N^2). Space: O(N)
12+
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
13+
intervals.sort(key=lambda val: (val[0], -val[1]))
14+
covered = [0] * len(intervals)
15+
for i, (start1, end1) in enumerate(intervals):
16+
if covered[i]: continue
17+
for j in range(i + 1, len(intervals)):
18+
start2, end2 = intervals[j]
19+
if start2 >= end1: break
20+
if start1 <= start2 and end2 <= end1:
21+
covered[j] = 1
22+
return len(covered) - sum(covered)
23+
24+
# 88 ms, 98.31%. Greedy. Time: O(NlogN). Space: O(1), O(N) if consider sorting memory
25+
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
26+
intervals.sort(key=lambda val: (val[0], -val[1]))
27+
prev_end = -1
28+
cnt = 0
29+
for start, end in intervals:
30+
if end > prev_end:
31+
cnt += 1
32+
prev_end = end
33+
return cnt
34+
# @lc code=end
35+

0 commit comments

Comments
 (0)