-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathLc 78
More file actions
37 lines (33 loc) · 1.04 KB
/
Lc 78
File metadata and controls
37 lines (33 loc) · 1.04 KB
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
https://leetcode.com/contest/biweekly-contest-78/problems/maximum-white-tiles-covered-by-a-carpet/
class Solution
{
public int maximumWhiteTiles(int[][] tiles, int carpetLen)
{
Arrays.sort(tiles,(a,b)->{return a[0]-b[0];});
int x = 0;
int y = 0;
long maxCount = 0;
long count = 0;
while(y < tiles.length)
{
long start = tiles[x][0];
long end = tiles[y][1];
if(end-start+1 <= carpetLen)
{
count += tiles[y][1] - tiles[y][0]+1;
maxCount = Math.max(maxCount,count);
y++;
}
else
{
long midDist = start+carpetLen-1;
long s = tiles[y][0];
long e = tiles[y][1];
if(midDist <= e && midDist >= s) maxCount = Math.max(maxCount,count+midDist-s+1);
count -= tiles[x][1] - tiles[x][0] + 1;
x++;
}
}
return (int)maxCount;
}
}