Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions solution/2800-2899/2848.Points That Intersect With Cars/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# [2848. 与车相交的点](https://leetcode.cn/problems/points-that-intersect-with-cars)

[English Version](/solution/2800-2899/2848.Points%20That%20Intersect%20With%20Cars/README_EN.md)

## 题目描述

<!-- 这里写题目描述 -->

<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>

<p>返回数轴上被车 <strong>任意部分</strong> 覆盖的整数点的数目。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<pre>
<strong>输入:</strong>nums = [[3,6],[1,5],[4,7]]
<strong>输出:</strong>7
<strong>解释:</strong>从 1 到 7 的所有点都至少与一辆车相交,因此答案为 7 。
</pre>

<p><strong class="example">示例 2:</strong></p>

<pre>
<strong>输入:</strong>nums = [[1,3],[5,8]]
<strong>输出:</strong>7
<strong>解释:</strong>1、2、3、5、6、7、8 共计 7 个点满足至少与一辆车相交,因此答案为 7 。
</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>nums[i].length == 2</code></li>
<li><code><font face="monospace">1 &lt;= start<sub>i</sub>&nbsp;&lt;= end<sub>i</sub>&nbsp;&lt;= 100</font></code></li>
</ul>

## 解法

<!-- 这里可写通用的实现逻辑 -->

**方法一:差分数组**

我们创建一个长度为 $110$ 的差分数组 $d$,然后遍历给定的数组,对于每个区间 $[a, b]$,我们令 $d[a]$ 增加 $1$,$d[b + 1]$ 减少 $1$。最后我们遍历差分数组 $d$,求每个位置的前缀和 $s$,如果 $s > 0$,则说明该位置被覆盖,我们将答案增加 $1$。

时间复杂度 $O(n)$,空间复杂度 $O(M)$。其中 $n$ 是给定数组的长度,而 $M$ 是数组中元素的最大值。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python
class Solution:
def numberOfPoints(self, nums: List[List[int]]) -> int:
d = [0] * 110
for a, b in nums:
d[a] += 1
d[b + 1] -= 1
return sum(s > 0 for s in accumulate(d))
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
public int numberOfPoints(List<List<Integer>> nums) {
int[] d = new int[110];
for (var e : nums) {
d[e.get(0)]++;
d[e.get(1) + 1]--;
}
int ans = 0, s = 0;
for (int x : d) {
s += x;
if (s > 0) {
ans++;
}
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int numberOfPoints(vector<vector<int>>& nums) {
int d[110]{};
for (auto& e : nums) {
d[e[0]]++;
d[e[1] + 1]--;
}
int ans = 0, s = 0;
for (int x : d) {
s += x;
ans += s > 0;
}
return ans;
}
};
```

### **Go**

```go
func numberOfPoints(nums [][]int) (ans int) {
d := [110]int{}
for _, e := range nums {
d[e[0]]++
d[e[1]+1]--
}
s := 0
for _, x := range d {
s += x
if s > 0 {
ans++
}
}
return
}
```

### **TypeScript**

```ts
function numberOfPoints(nums: number[][]): number {
const d: number[] = Array(110).fill(0);
for (const [a, b] of nums) {
d[a]++;
d[b + 1]--;
}
let ans = 0;
let s = 0;
for (const x of d) {
s += x;
if (s > 0) {
ans++;
}
}
return ans;
}
```

### **...**

```

```

<!-- tabs:end -->
143 changes: 143 additions & 0 deletions solution/2800-2899/2848.Points That Intersect With Cars/README_EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# [2848. Points That Intersect With Cars](https://leetcode.com/problems/points-that-intersect-with-cars)

[中文文档](/solution/2800-2899/2848.Points%20That%20Intersect%20With%20Cars/README.md)

## Description

<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>

<p>Return <em>the number of integer points on the line that are covered with <strong>any part</strong> of a car.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [[3,6],[1,5],[4,7]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> All the points from 1 to 7 intersect at least one car, therefore the answer would be 7.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [[1,3],[5,8]]
<strong>Output:</strong> 7
<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.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>nums[i].length == 2</code></li>
<li><code><font face="monospace">1 &lt;= start<sub>i</sub>&nbsp;&lt;= end<sub>i</sub>&nbsp;&lt;= 100</font></code></li>
</ul>

## Solutions

<!-- tabs:start -->

### **Python3**

```python
class Solution:
def numberOfPoints(self, nums: List[List[int]]) -> int:
d = [0] * 110
for a, b in nums:
d[a] += 1
d[b + 1] -= 1
return sum(s > 0 for s in accumulate(d))
```

### **Java**

```java
class Solution {
public int numberOfPoints(List<List<Integer>> nums) {
int[] d = new int[110];
for (var e : nums) {
d[e.get(0)]++;
d[e.get(1) + 1]--;
}
int ans = 0, s = 0;
for (int x : d) {
s += x;
if (s > 0) {
ans++;
}
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int numberOfPoints(vector<vector<int>>& nums) {
int d[110]{};
for (auto& e : nums) {
d[e[0]]++;
d[e[1] + 1]--;
}
int ans = 0, s = 0;
for (int x : d) {
s += x;
ans += s > 0;
}
return ans;
}
};
```

### **Go**

```go
func numberOfPoints(nums [][]int) (ans int) {
d := [110]int{}
for _, e := range nums {
d[e[0]]++
d[e[1]+1]--
}
s := 0
for _, x := range d {
s += x
if s > 0 {
ans++
}
}
return
}
```

### **TypeScript**

```ts
function numberOfPoints(nums: number[][]): number {
const d: number[] = Array(110).fill(0);
for (const [a, b] of nums) {
d[a]++;
d[b + 1]--;
}
let ans = 0;
let s = 0;
for (const x of d) {
s += x;
if (s > 0) {
ans++;
}
}
return ans;
}
```

### **...**

```

```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int numberOfPoints(vector<vector<int>>& nums) {
int d[110]{};
for (auto& e : nums) {
d[e[0]]++;
d[e[1] + 1]--;
}
int ans = 0, s = 0;
for (int x : d) {
s += x;
ans += s > 0;
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
func numberOfPoints(nums [][]int) (ans int) {
d := [110]int{}
for _, e := range nums {
d[e[0]]++
d[e[1]+1]--
}
s := 0
for _, x := range d {
s += x
if s > 0 {
ans++
}
}
return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int numberOfPoints(List<List<Integer>> nums) {
int[] d = new int[110];
for (var e : nums) {
d[e.get(0)]++;
d[e.get(1) + 1]--;
}
int ans = 0, s = 0;
for (int x : d) {
s += x;
if (s > 0) {
ans++;
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution:
def numberOfPoints(self, nums: List[List[int]]) -> int:
d = [0] * 110
for a, b in nums:
d[a] += 1
d[b + 1] -= 1
return sum(s > 0 for s in accumulate(d))
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function numberOfPoints(nums: number[][]): number {
const d: number[] = Array(110).fill(0);
for (const [a, b] of nums) {
d[a]++;
d[b + 1]--;
}
let ans = 0;
let s = 0;
for (const x of d) {
s += x;
if (s > 0) {
ans++;
}
}
return ans;
}
Loading