Skip to content

Commit 5857549

Browse files
authored
Create 2271-maximum-white-tiles-covered-by-a-carpet.js
1 parent 2fba763 commit 5857549

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[][]} tiles
3+
* @param {number} carpetLen
4+
* @return {number}
5+
*/
6+
const maximumWhiteTiles = function (tiles, carpetLen) {
7+
const sorted = tiles.sort((a, b) => a[0] - b[0])
8+
let res = 0
9+
10+
let total = 0
11+
let right = 0
12+
13+
for (let tile of sorted) {
14+
const start = tile[0]
15+
const end = start + carpetLen - 1
16+
while (right < sorted.length && tiles[right][1] < end) {
17+
total += tiles[right][1] - tiles[right][0] + 1
18+
right++
19+
}
20+
if (right === sorted.length || sorted[right][0] > end) {
21+
res = Math.max(res, total)
22+
} else {
23+
res = Math.max(res, total + (end - tiles[right][0] + 1))
24+
}
25+
total -= tile[1] - tile[0] + 1
26+
}
27+
28+
return res
29+
}

0 commit comments

Comments
 (0)