|
| 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> </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> </p> |
| 29 | +<p><strong>Constraints:</strong></p> |
| 30 | + |
| 31 | +<ul> |
| 32 | + <li><code>1 <= nums.length <= 100</code></li> |
| 33 | + <li><code>nums[i].length == 2</code></li> |
| 34 | + <li><code><font face="monospace">1 <= start<sub>i</sub> <= end<sub>i</sub> <= 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 --> |
0 commit comments