Skip to content

Commit 93fd707

Browse files
authored
feat: add solutions to lc problems: No.2848~2850 (#1593)
* No.2848.Points That Intersect With Cars * No.2849.Determine if a Cell Is Reachable at a Given Time * No.2850.Minimum Moves to Spread Stones Over Grid
1 parent 30dabe0 commit 93fd707

30 files changed

+1396
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# [2848. 与车相交的点](https://leetcode.cn/problems/points-that-intersect-with-cars)
2+
3+
[English Version](/solution/2800-2899/2848.Points%20That%20Intersect%20With%20Cars/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的二维整数数组 <code>nums</code> 表示汽车停放在数轴上的坐标。对于任意下标 <code>i</code>,<code>nums[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> ,其中 <code>start<sub>i</sub></code> 是第 <code>i</code> 辆车的起点,<code>end<sub>i</sub></code> 是第 <code>i</code> 辆车的终点。</p>
10+
11+
<p>返回数轴上被车 <strong>任意部分</strong> 覆盖的整数点的数目。</p>
12+
13+
<p>&nbsp;</p>
14+
15+
<p><strong class="example">示例 1:</strong></p>
16+
17+
<pre>
18+
<strong>输入:</strong>nums = [[3,6],[1,5],[4,7]]
19+
<strong>输出:</strong>7
20+
<strong>解释:</strong>从 1 到 7 的所有点都至少与一辆车相交,因此答案为 7 。
21+
</pre>
22+
23+
<p><strong class="example">示例 2:</strong></p>
24+
25+
<pre>
26+
<strong>输入:</strong>nums = [[1,3],[5,8]]
27+
<strong>输出:</strong>7
28+
<strong>解释:</strong>1、2、3、5、6、7、8 共计 7 个点满足至少与一辆车相交,因此答案为 7 。
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
33+
<p><strong>提示:</strong></p>
34+
35+
<ul>
36+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
37+
<li><code>nums[i].length == 2</code></li>
38+
<li><code><font face="monospace">1 &lt;= start<sub>i</sub>&nbsp;&lt;= end<sub>i</sub>&nbsp;&lt;= 100</font></code></li>
39+
</ul>
40+
41+
## 解法
42+
43+
<!-- 这里可写通用的实现逻辑 -->
44+
45+
**方法一:差分数组**
46+
47+
我们创建一个长度为 $110$ 的差分数组 $d$,然后遍历给定的数组,对于每个区间 $[a, b]$,我们令 $d[a]$ 增加 $1$,$d[b + 1]$ 减少 $1$。最后我们遍历差分数组 $d$,求每个位置的前缀和 $s$,如果 $s > 0$,则说明该位置被覆盖,我们将答案增加 $1$。
48+
49+
时间复杂度 $O(n)$,空间复杂度 $O(M)$。其中 $n$ 是给定数组的长度,而 $M$ 是数组中元素的最大值。
50+
51+
<!-- tabs:start -->
52+
53+
### **Python3**
54+
55+
<!-- 这里可写当前语言的特殊实现逻辑 -->
56+
57+
```python
58+
class Solution:
59+
def numberOfPoints(self, nums: List[List[int]]) -> int:
60+
d = [0] * 110
61+
for a, b in nums:
62+
d[a] += 1
63+
d[b + 1] -= 1
64+
return sum(s > 0 for s in accumulate(d))
65+
```
66+
67+
### **Java**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```java
72+
class Solution {
73+
public int numberOfPoints(List<List<Integer>> nums) {
74+
int[] d = new int[110];
75+
for (var e : nums) {
76+
d[e.get(0)]++;
77+
d[e.get(1) + 1]--;
78+
}
79+
int ans = 0, s = 0;
80+
for (int x : d) {
81+
s += x;
82+
if (s > 0) {
83+
ans++;
84+
}
85+
}
86+
return ans;
87+
}
88+
}
89+
```
90+
91+
### **C++**
92+
93+
```cpp
94+
class Solution {
95+
public:
96+
int numberOfPoints(vector<vector<int>>& nums) {
97+
int d[110]{};
98+
for (auto& e : nums) {
99+
d[e[0]]++;
100+
d[e[1] + 1]--;
101+
}
102+
int ans = 0, s = 0;
103+
for (int x : d) {
104+
s += x;
105+
ans += s > 0;
106+
}
107+
return ans;
108+
}
109+
};
110+
```
111+
112+
### **Go**
113+
114+
```go
115+
func numberOfPoints(nums [][]int) (ans int) {
116+
d := [110]int{}
117+
for _, e := range nums {
118+
d[e[0]]++
119+
d[e[1]+1]--
120+
}
121+
s := 0
122+
for _, x := range d {
123+
s += x
124+
if s > 0 {
125+
ans++
126+
}
127+
}
128+
return
129+
}
130+
```
131+
132+
### **TypeScript**
133+
134+
```ts
135+
function numberOfPoints(nums: number[][]): number {
136+
const d: number[] = Array(110).fill(0);
137+
for (const [a, b] of nums) {
138+
d[a]++;
139+
d[b + 1]--;
140+
}
141+
let ans = 0;
142+
let s = 0;
143+
for (const x of d) {
144+
s += x;
145+
if (s > 0) {
146+
ans++;
147+
}
148+
}
149+
return ans;
150+
}
151+
```
152+
153+
### **...**
154+
155+
```
156+
157+
```
158+
159+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# [2848. Points That Intersect With Cars](https://leetcode.com/problems/points-that-intersect-with-cars)
2+
3+
[中文文档](/solution/2800-2899/2848.Points%20That%20Intersect%20With%20Cars/README.md)
4+
5+
## Description
6+
7+
<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>
8+
9+
<p>Return <em>the number of integer points on the line that are covered with <strong>any part</strong> of a car.</em></p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> nums = [[3,6],[1,5],[4,7]]
16+
<strong>Output:</strong> 7
17+
<strong>Explanation:</strong> All the points from 1 to 7 intersect at least one car, therefore the answer would be 7.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> nums = [[1,3],[5,8]]
24+
<strong>Output:</strong> 7
25+
<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.
26+
</pre>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
33+
<li><code>nums[i].length == 2</code></li>
34+
<li><code><font face="monospace">1 &lt;= start<sub>i</sub>&nbsp;&lt;= end<sub>i</sub>&nbsp;&lt;= 100</font></code></li>
35+
</ul>
36+
37+
## Solutions
38+
39+
<!-- tabs:start -->
40+
41+
### **Python3**
42+
43+
```python
44+
class Solution:
45+
def numberOfPoints(self, nums: List[List[int]]) -> int:
46+
d = [0] * 110
47+
for a, b in nums:
48+
d[a] += 1
49+
d[b + 1] -= 1
50+
return sum(s > 0 for s in accumulate(d))
51+
```
52+
53+
### **Java**
54+
55+
```java
56+
class Solution {
57+
public int numberOfPoints(List<List<Integer>> nums) {
58+
int[] d = new int[110];
59+
for (var e : nums) {
60+
d[e.get(0)]++;
61+
d[e.get(1) + 1]--;
62+
}
63+
int ans = 0, s = 0;
64+
for (int x : d) {
65+
s += x;
66+
if (s > 0) {
67+
ans++;
68+
}
69+
}
70+
return ans;
71+
}
72+
}
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
int numberOfPoints(vector<vector<int>>& nums) {
81+
int d[110]{};
82+
for (auto& e : nums) {
83+
d[e[0]]++;
84+
d[e[1] + 1]--;
85+
}
86+
int ans = 0, s = 0;
87+
for (int x : d) {
88+
s += x;
89+
ans += s > 0;
90+
}
91+
return ans;
92+
}
93+
};
94+
```
95+
96+
### **Go**
97+
98+
```go
99+
func numberOfPoints(nums [][]int) (ans int) {
100+
d := [110]int{}
101+
for _, e := range nums {
102+
d[e[0]]++
103+
d[e[1]+1]--
104+
}
105+
s := 0
106+
for _, x := range d {
107+
s += x
108+
if s > 0 {
109+
ans++
110+
}
111+
}
112+
return
113+
}
114+
```
115+
116+
### **TypeScript**
117+
118+
```ts
119+
function numberOfPoints(nums: number[][]): number {
120+
const d: number[] = Array(110).fill(0);
121+
for (const [a, b] of nums) {
122+
d[a]++;
123+
d[b + 1]--;
124+
}
125+
let ans = 0;
126+
let s = 0;
127+
for (const x of d) {
128+
s += x;
129+
if (s > 0) {
130+
ans++;
131+
}
132+
}
133+
return ans;
134+
}
135+
```
136+
137+
### **...**
138+
139+
```
140+
141+
```
142+
143+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int numberOfPoints(vector<vector<int>>& nums) {
4+
int d[110]{};
5+
for (auto& e : nums) {
6+
d[e[0]]++;
7+
d[e[1] + 1]--;
8+
}
9+
int ans = 0, s = 0;
10+
for (int x : d) {
11+
s += x;
12+
ans += s > 0;
13+
}
14+
return ans;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func numberOfPoints(nums [][]int) (ans int) {
2+
d := [110]int{}
3+
for _, e := range nums {
4+
d[e[0]]++
5+
d[e[1]+1]--
6+
}
7+
s := 0
8+
for _, x := range d {
9+
s += x
10+
if s > 0 {
11+
ans++
12+
}
13+
}
14+
return
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int numberOfPoints(List<List<Integer>> nums) {
3+
int[] d = new int[110];
4+
for (var e : nums) {
5+
d[e.get(0)]++;
6+
d[e.get(1) + 1]--;
7+
}
8+
int ans = 0, s = 0;
9+
for (int x : d) {
10+
s += x;
11+
if (s > 0) {
12+
ans++;
13+
}
14+
}
15+
return ans;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def numberOfPoints(self, nums: List[List[int]]) -> int:
3+
d = [0] * 110
4+
for a, b in nums:
5+
d[a] += 1
6+
d[b + 1] -= 1
7+
return sum(s > 0 for s in accumulate(d))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function numberOfPoints(nums: number[][]): number {
2+
const d: number[] = Array(110).fill(0);
3+
for (const [a, b] of nums) {
4+
d[a]++;
5+
d[b + 1]--;
6+
}
7+
let ans = 0;
8+
let s = 0;
9+
for (const x of d) {
10+
s += x;
11+
if (s > 0) {
12+
ans++;
13+
}
14+
}
15+
return ans;
16+
}

0 commit comments

Comments
 (0)