-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay 17.2.txt
57 lines (43 loc) · 1.49 KB
/
Day 17.2.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
1893. Check if All the Integers in a Range Are Covered
You are given a 2D integer array ranges and two integers left and right. Each ranges[i] = [starti, endi] represents an inclusive interval between starti and endi.
Return true if each integer in the inclusive range [left, right] is covered by at least one interval in ranges. Return false otherwise.
An integer x is covered by an interval ranges[i] = [starti, endi] if starti <= x <= endi.
Example 1:
Input: ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5
Output: true
Explanation: Every integer between 2 and 5 is covered:
- 2 is covered by the first range.
- 3 and 4 are covered by the second range.
- 5 is covered by the third range.
Example 2:
Input: ranges = [[1,10],[10,20]], left = 21, right = 21
Output: false
Explanation: 21 is not covered by any range.
Constraints:
1 <= ranges.length <= 50
1 <= starti <= endi <= 50
1 <= left <= right <= 50
class Solution {
public boolean isCovered(int[][] r, int l, int k) {
HashMap<Integer,Integer> nm=new HashMap<>();
int i,j,c=0,h;
for(i=0;i<r.length;i++)
{
h=r[i][1];
for(j=r[i][0];j<=h;j++)
{
nm.put(j,1);
}
}
nm.putAll(nm);
for(i=l;i<=k;i++)
{
if(nm.containsKey(i))
{
nm.remove(i);
c++;
}
}
return c==(k+1-l) ? true : false;
}
}