Skip to content

Commit c760ab5

Browse files
committed
Sync LeetCode submission Runtime - 7 ms (34.82%), Memory - 17.8 MB (6.29%)
1 parent 40f7aa7 commit c760ab5

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>nums</code> representing the coordinates of the cars parking on a number line. For any index <code>i</code>, <code>nums[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> where <code>start<sub>i</sub></code> is the starting point of the <code>i<sup>th</sup></code> car and <code>end<sub>i</sub></code> is the ending point of the <code>i<sup>th</sup></code> car.</p>
2+
3+
<p>Return <em>the number of integer points on the line that are covered with <strong>any part</strong> of a car.</em></p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> nums = [[3,6],[1,5],[4,7]]
10+
<strong>Output:</strong> 7
11+
<strong>Explanation:</strong> All the points from 1 to 7 intersect at least one car, therefore the answer would be 7.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [[1,3],[5,8]]
18+
<strong>Output:</strong> 7
19+
<strong>Explanation:</strong> Points intersecting at least one car are 1, 2, 3, 5, 6, 7, 8. There are a total of 7 points, therefore the answer would be 7.
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Constraints:</strong></p>
24+
25+
<ul>
26+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
27+
<li><code>nums[i].length == 2</code></li>
28+
<li><code><font face="monospace">1 &lt;= start<sub>i</sub>&nbsp;&lt;= end<sub>i</sub>&nbsp;&lt;= 100</font></code></li>
29+
</ul>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Time: O(n * m), n = no. of cars, m = max range of any car
2+
# Space: O(p), p = unique points covered by all cars
3+
4+
class Solution:
5+
def numberOfPoints(self, nums: List[List[int]]) -> int:
6+
covered_points = set()
7+
8+
for start, end in nums:
9+
for point in range(start, end + 1):
10+
covered_points.add(point)
11+
12+
return len(covered_points)

0 commit comments

Comments
 (0)