We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2fba763 commit 5857549Copy full SHA for 5857549
2271-maximum-white-tiles-covered-by-a-carpet.js
@@ -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