Skip to content

Commit ada8e80

Browse files
authored
Update 2271-maximum-white-tiles-covered-by-a-carpet.js
1 parent 5857549 commit ada8e80

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

2271-maximum-white-tiles-covered-by-a-carpet.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,34 @@ const maximumWhiteTiles = function (tiles, carpetLen) {
2727

2828
return res
2929
}
30+
31+
// another
32+
33+
/**
34+
* @param {number[][]} tiles
35+
* @param {number} carpetLen
36+
* @return {number}
37+
*/
38+
const maximumWhiteTiles = function (tiles, carpetLen) {
39+
tiles.sort((a, b) => a[0] - b[0])
40+
let res = 0, total = 0, right = 0
41+
const n = tiles.length
42+
for(let i = 0; i < n; i++) {
43+
const [l, r] = tiles[i]
44+
const end = l + carpetLen - 1
45+
while(right < n && tiles[right][1] <= end) {
46+
total += tiles[right][1] - tiles[right][0] + 1
47+
right++
48+
}
49+
50+
if(right === n || tiles[right][0] > end) {
51+
res = Math.max(res, total)
52+
} else {
53+
res = Math.max(res, total + end - tiles[right][0] + 1)
54+
}
55+
56+
total -= r - l + 1
57+
}
58+
59+
return res
60+
}

0 commit comments

Comments
 (0)