comments | difficulty | edit_url | rating | source | tags | |||
---|---|---|---|---|---|---|---|---|
true |
Easy |
1229 |
Weekly Contest 362 Q1 |
|
You are given a 0-indexed 2D integer array nums
representing the coordinates of the cars parking on a number line. For any index i
, nums[i] = [starti, endi]
where starti
is the starting point of the ith
car and endi
is the ending point of the ith
car.
Return the number of integer points on the line that are covered with any part of a car.
Example 1:
Input: nums = [[3,6],[1,5],[4,7]] Output: 7 Explanation: All the points from 1 to 7 intersect at least one car, therefore the answer would be 7.
Example 2:
Input: nums = [[1,3],[5,8]] Output: 7 Explanation: 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.
Constraints:
1 <= nums.length <= 100
nums[i].length == 2
1 <= starti <= endi <= 100
According to the problem description, we need to add one vehicle to each interval
We define an array
Finally, we perform a prefix sum operation on
The time complexity is
class Solution:
def numberOfPoints(self, nums: List[List[int]]) -> int:
m = 102
d = [0] * m
for start, end in nums:
d[start] += 1
d[end + 1] -= 1
return sum(s > 0 for s in accumulate(d))
class Solution {
public int numberOfPoints(List<List<Integer>> nums) {
int[] d = new int[102];
for (var e : nums) {
int start = e.get(0), end = e.get(1);
++d[start];
--d[end + 1];
}
int ans = 0, s = 0;
for (int x : d) {
s += x;
if (s > 0) {
++ans;
}
}
return ans;
}
}
class Solution {
public:
int numberOfPoints(vector<vector<int>>& nums) {
int d[102]{};
for (const auto& e : nums) {
int start = e[0], end = e[1];
++d[start];
--d[end + 1];
}
int ans = 0, s = 0;
for (int x : d) {
s += x;
ans += s > 0;
}
return ans;
}
};
func numberOfPoints(nums [][]int) (ans int) {
d := [102]int{}
for _, e := range nums {
start, end := e[0], e[1]
d[start]++
d[end+1]--
}
s := 0
for _, x := range d {
s += x
if s > 0 {
ans++
}
}
return
}
function numberOfPoints(nums: number[][]): number {
const d: number[] = Array(102).fill(0);
for (const [start, end] of nums) {
++d[start];
--d[end + 1];
}
let ans = 0;
let s = 0;
for (const x of d) {
s += x;
ans += s > 0 ? 1 : 0;
}
return ans;
}
If the range of intervals in the problem is large, we can use a hash table to store the start and end points of the intervals. Then, we sort the keys of the hash table and perform prefix sum statistics.
The time complexity is
class Solution:
def numberOfPoints(self, nums: List[List[int]]) -> int:
d = defaultdict(int)
for start, end in nums:
d[start] += 1
d[end + 1] -= 1
ans = s = last = 0
for cur, v in sorted(d.items()):
if s > 0:
ans += cur - last
s += v
last = cur
return ans
class Solution {
public int numberOfPoints(List<List<Integer>> nums) {
TreeMap<Integer, Integer> d = new TreeMap<>();
for (var e : nums) {
int start = e.get(0), end = e.get(1);
d.merge(start, 1, Integer::sum);
d.merge(end + 1, -1, Integer::sum);
}
int ans = 0, s = 0, last = 0;
for (var e : d.entrySet()) {
int cur = e.getKey(), v = e.getValue();
if (s > 0) {
ans += cur - last;
}
s += v;
last = cur;
}
return ans;
}
}
class Solution {
public:
int numberOfPoints(vector<vector<int>>& nums) {
map<int, int> d;
for (const auto& e : nums) {
int start = e[0], end = e[1];
++d[start];
--d[end + 1];
}
int ans = 0, s = 0, last = 0;
for (const auto& [cur, v] : d) {
if (s > 0) {
ans += cur - last;
}
s += v;
last = cur;
}
return ans;
}
};
func numberOfPoints(nums [][]int) (ans int) {
d := map[int]int{}
for _, e := range nums {
start, end := e[0], e[1]
d[start]++
d[end+1]--
}
keys := []int{}
for k := range d {
keys = append(keys, k)
}
s, last := 0, 0
sort.Ints(keys)
for _, cur := range keys {
if s > 0 {
ans += cur - last
}
s += d[cur]
last = cur
}
return
}
function numberOfPoints(nums: number[][]): number {
const d = new Map<number, number>();
for (const [start, end] of nums) {
d.set(start, (d.get(start) || 0) + 1);
d.set(end + 1, (d.get(end + 1) || 0) - 1);
}
const keys = [...d.keys()].sort((a, b) => a - b);
let [ans, s, last] = [0, 0, 0];
for (const cur of keys) {
if (s > 0) {
ans += cur - last;
}
s += d.get(cur)!;
last = cur;
}
return ans;
}