Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 2D integer array tiles where tiles[i] = [li, ri] represents that every tile j in the range li <= j <= ri is colored white.
You are also given an integer carpetLen, the length of a single carpet that can be placed anywhere.
Return the maximum number of white tiles that can be covered by the carpet.
Example 1:
Input: tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10 Output: 9 Explanation: Place the carpet starting on tile 10. It covers 9 white tiles, so we return 9. Note that there may be other places where the carpet covers 9 white tiles. It can be shown that the carpet cannot cover more than 9 white tiles.
Example 2:
Input: tiles = [[10,11],[1,1]], carpetLen = 2 Output: 2 Explanation: Place the carpet starting on tile 10. It covers 2 white tiles, so we return 2.
Constraints:
1 <= tiles.length <= 5 * 104tiles[i].length == 21 <= li <= ri <= 1091 <= carpetLen <= 109tiles are non-overlapping.Problem summary: You are given a 2D integer array tiles where tiles[i] = [li, ri] represents that every tile j in the range li <= j <= ri is colored white. You are also given an integer carpetLen, the length of a single carpet that can be placed anywhere. Return the maximum number of white tiles that can be covered by the carpet.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Greedy · Sliding Window
[[1,5],[10,11],[12,18],[20,25],[30,32]] 10
[[10,11],[1,1]] 2
maximum-number-of-vowels-in-a-substring-of-given-length)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2271: Maximum White Tiles Covered by a Carpet
class Solution {
public int maximumWhiteTiles(int[][] tiles, int carpetLen) {
Arrays.sort(tiles, (a, b) -> a[0] - b[0]);
int n = tiles.length;
int s = 0, ans = 0;
for (int i = 0, j = 0; i < n; ++i) {
while (j < n && tiles[j][1] - tiles[i][0] + 1 <= carpetLen) {
s += tiles[j][1] - tiles[j][0] + 1;
++j;
}
if (j < n && tiles[i][0] + carpetLen > tiles[j][0]) {
ans = Math.max(ans, s + tiles[i][0] + carpetLen - tiles[j][0]);
} else {
ans = Math.max(ans, s);
}
s -= (tiles[i][1] - tiles[i][0] + 1);
}
return ans;
}
}
// Accepted solution for LeetCode #2271: Maximum White Tiles Covered by a Carpet
func maximumWhiteTiles(tiles [][]int, carpetLen int) int {
sort.Slice(tiles, func(i, j int) bool { return tiles[i][0] < tiles[j][0] })
n := len(tiles)
s, ans := 0, 0
for i, j := 0, 0; i < n; i++ {
for j < n && tiles[j][1]-tiles[i][0]+1 <= carpetLen {
s += tiles[j][1] - tiles[j][0] + 1
j++
}
if j < n && tiles[i][0]+carpetLen > tiles[j][0] {
ans = max(ans, s+tiles[i][0]+carpetLen-tiles[j][0])
} else {
ans = max(ans, s)
}
s -= (tiles[i][1] - tiles[i][0] + 1)
}
return ans
}
# Accepted solution for LeetCode #2271: Maximum White Tiles Covered by a Carpet
class Solution:
def maximumWhiteTiles(self, tiles: List[List[int]], carpetLen: int) -> int:
tiles.sort()
n = len(tiles)
s = ans = j = 0
for i, (li, ri) in enumerate(tiles):
while j < n and tiles[j][1] - li + 1 <= carpetLen:
s += tiles[j][1] - tiles[j][0] + 1
j += 1
if j < n and li + carpetLen > tiles[j][0]:
ans = max(ans, s + li + carpetLen - tiles[j][0])
else:
ans = max(ans, s)
s -= ri - li + 1
return ans
// Accepted solution for LeetCode #2271: Maximum White Tiles Covered by a Carpet
/**
* [2271] Maximum White Tiles Covered by a Carpet
*
* You are given a 2D integer array tiles where tiles[i] = [li, ri] represents that every tile j in the range li <= j <= ri is colored white.
* You are also given an integer carpetLen, the length of a single carpet that can be placed anywhere.
* Return the maximum number of white tiles that can be covered by the carpet.
*
* Example 1:
* <img alt="" src="https://assets.leetcode.com/uploads/2022/03/25/example1drawio3.png" style="width: 644px; height: 158px;" />
* Input: tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10
* Output: 9
* Explanation: Place the carpet starting on tile 10.
* It covers 9 white tiles, so we return 9.
* Note that there may be other places where the carpet covers 9 white tiles.
* It can be shown that the carpet cannot cover more than 9 white tiles.
*
* Example 2:
* <img alt="" src="https://assets.leetcode.com/uploads/2022/03/24/example2drawio.png" style="width: 231px; height: 168px;" />
* Input: tiles = [[10,11],[1,1]], carpetLen = 2
* Output: 2
* Explanation: Place the carpet starting on tile 10.
* It covers 2 white tiles, so we return 2.
*
*
* Constraints:
*
* 1 <= tiles.length <= 5 * 10^4
* tiles[i].length == 2
* 1 <= li <= ri <= 10^9
* 1 <= carpetLen <= 10^9
* The tiles are non-overlapping.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/
// discuss: https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn maximum_white_tiles(tiles: Vec<Vec<i32>>, carpet_len: i32) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2271_example_1() {
let tiles = vec![
vec![1, 5],
vec![10, 11],
vec![12, 18],
vec![20, 25],
vec![30, 32],
];
let carpet_len = 10;
let result = 9;
assert_eq!(Solution::maximum_white_tiles(tiles, carpet_len), result);
}
#[test]
#[ignore]
fn test_2271_example_2() {
let tiles = vec![vec![10, 11], vec![1, 1]];
let carpet_len = 10;
let result = 2;
assert_eq!(Solution::maximum_white_tiles(tiles, carpet_len), result);
}
}
// Accepted solution for LeetCode #2271: Maximum White Tiles Covered by a Carpet
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2271: Maximum White Tiles Covered by a Carpet
// class Solution {
// public int maximumWhiteTiles(int[][] tiles, int carpetLen) {
// Arrays.sort(tiles, (a, b) -> a[0] - b[0]);
// int n = tiles.length;
// int s = 0, ans = 0;
// for (int i = 0, j = 0; i < n; ++i) {
// while (j < n && tiles[j][1] - tiles[i][0] + 1 <= carpetLen) {
// s += tiles[j][1] - tiles[j][0] + 1;
// ++j;
// }
// if (j < n && tiles[i][0] + carpetLen > tiles[j][0]) {
// ans = Math.max(ans, s + tiles[i][0] + carpetLen - tiles[j][0]);
// } else {
// ans = Math.max(ans, s);
// }
// s -= (tiles[i][1] - tiles[i][0] + 1);
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.