Skip to content

Commit 3dc2a4b

Browse files
authored
Merge pull request #3675 from AmruthaPariprolu/feature/2271
solution added to 2271
2 parents 2b05461 + f17b216 commit 3dc2a4b

File tree

1 file changed

+329
-0
lines changed

1 file changed

+329
-0
lines changed
Lines changed: 329 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,329 @@
1+
---
2+
id: Maximum-White-Tiles-Covered-by-a-Carpet
3+
title: Maximum White Tiles Covered by a Carpet
4+
sidebar_label: 2271-Maximum White Tiles Covered by a Carpet
5+
tags:
6+
- Arrays
7+
- Brute Force
8+
- Optimized approach
9+
- LeetCode
10+
- Python
11+
- Java
12+
- C++
13+
14+
description: "This is a solution to Maximum White Tiles Covered by a Carpet problem on LeetCode."
15+
sidebar_position: 72
16+
---
17+
18+
## Problem Statement
19+
In this tutorial, we will solve the Maximum White Tiles Covered by a Carpet problem . We will provide the implementation of the solution in Python, Java, and C++.
20+
21+
### Problem Description
22+
23+
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.
24+
25+
You are also given an integer carpetLen, the length of a single carpet that can be placed anywhere.
26+
27+
Return the maximum number of white tiles that can be covered by the carpet.
28+
29+
### Examples
30+
31+
**Example 1:**
32+
Input: tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10
33+
Output: 9
34+
Explanation: Place the carpet starting on tile 10.
35+
It covers 9 white tiles, so we return 9.
36+
Note that there may be other places where the carpet covers 9 white tiles.
37+
It can be shown that the carpet cannot cover more than 9 white tiles.
38+
**Example 2:**
39+
Input: tiles = [[10,11],[1,1]], carpetLen = 2
40+
Output: 2
41+
Explanation: Place the carpet starting on tile 10.
42+
It covers 2 white tiles, so we return 2.
43+
### Constraints
44+
- `1 <= tiles.length <= 5 * 104`
45+
- `tiles[i].length == 2`
46+
- `1 <= li <= ri <= 109`
47+
- `1 <= carpetLen <= 109`
48+
- `The tiles are non-overlapping.`
49+
## Solution of Given Problem
50+
51+
### Intuition and Approach
52+
53+
The problem can be solved using a brute force approach or an optimized Technique.
54+
55+
<Tabs>
56+
<tabItem value="Brute Force" label="Brute Force">
57+
58+
### Approach 1:Brute Force (Naive)
59+
60+
61+
Brute Force Approach: In the brute force approach, we try placing the carpet starting at every possible position and calculate the number of tiles it covers. This approach is inefficient, especially for large inputs, but it provides a straightforward way to understand the problem.
62+
63+
#### Codes in Different Languages
64+
65+
<Tabs>
66+
<TabItem value="C++" label="C++" default>
67+
<SolutionAuthor name="@AmruthaPariprolu"/>
68+
69+
```cpp
70+
#include <vector>
71+
#include <algorithm>
72+
#include <iostream>
73+
74+
int maxWhiteTilesBruteForce(std::vector<std::vector<int>>& tiles, int carpetLen) {
75+
std::sort(tiles.begin(), tiles.end());
76+
int maxCovered = 0;
77+
78+
for (const auto& tile : tiles) {
79+
int start = tile[0];
80+
int end = start + carpetLen - 1;
81+
int covered = 0;
82+
83+
for (const auto& t : tiles) {
84+
if (t[1] < start) continue;
85+
if (t[0] > end) break;
86+
covered += std::min(t[1], end) - std::max(t[0], start) + 1;
87+
}
88+
maxCovered = std::max(maxCovered, covered);
89+
}
90+
return maxCovered;
91+
}
92+
93+
int main() {
94+
std::vector<std::vector<int>> tiles = {{1, 5}, {10, 11}, {12, 18}, {20, 25}, {30, 32}};
95+
int carpetLen = 10;
96+
std::cout << "Maximum white tiles covered: " << maxWhiteTilesBruteForce(tiles, carpetLen) << std::endl;
97+
return 0;
98+
}
99+
100+
101+
```
102+
</TabItem>
103+
<TabItem value="Java" label="Java">
104+
<SolutionAuthor name="@AmruthaPariprolu"/>
105+
106+
```java
107+
import java.util.*;
108+
109+
public class Main {
110+
public static int maxWhiteTilesBruteForce(int[][] tiles, int carpetLen) {
111+
Arrays.sort(tiles, Comparator.comparingInt(a -> a[0]));
112+
int maxCovered = 0;
113+
114+
for (int[] tile : tiles) {
115+
int start = tile[0];
116+
int end = start + carpetLen - 1;
117+
int covered = 0;
118+
119+
for (int[] t : tiles) {
120+
if (t[1] < start) continue;
121+
if (t[0] > end) break;
122+
covered += Math.min(t[1], end) - Math.max(t[0], start) + 1;
123+
}
124+
maxCovered = Math.max(maxCovered, covered);
125+
}
126+
return maxCovered;
127+
}
128+
129+
public static void main(String[] args) {
130+
int[][] tiles = {{1, 5}, {10, 11}, {12, 18}, {20, 25}, {30, 32}};
131+
int carpetLen = 10;
132+
System.out.println("Maximum white tiles covered: " + maxWhiteTilesBruteForce(tiles, carpetLen));
133+
}
134+
}
135+
136+
137+
138+
```
139+
140+
141+
</TabItem>
142+
<TabItem value="Python" label="Python">
143+
<SolutionAuthor name="@AmruthaPariprolu"/>
144+
145+
```python
146+
def max_white_tiles_brute_force(tiles, carpet_len):
147+
tiles.sort()
148+
max_covered = 0
149+
150+
for start, _ in tiles:
151+
end = start + carpet_len - 1
152+
covered = 0
153+
154+
for l, r in tiles:
155+
if r < start:
156+
continue
157+
if l > end:
158+
break
159+
covered += min(r, end) - max(l, start) + 1
160+
161+
max_covered = max(max_covered, covered)
162+
return max_covered
163+
164+
tiles = [[1, 5], [10, 11], [12, 18], [20, 25], [30, 32]]
165+
carpet_len = 10
166+
print("Maximum white tiles covered:", max_white_tiles_brute_force(tiles, carpet_len))
167+
168+
169+
```
170+
171+
</TabItem>
172+
</Tabs>
173+
174+
175+
### Complexity Analysis
176+
177+
- Time Complexity: $O(n^2)$
178+
- because we iterate through all possible start points and check each tile range for each starting point.
179+
- Space Complexity: $O(1)$
180+
- as we only use a few extra variables for counting.
181+
182+
</tabItem>
183+
<tabItem value="Optimized approach" label="Optimized approach">
184+
185+
### Approach 2: Optimized approach
186+
187+
Optimized Approach: The optimized approach uses a sliding window technique to find the maximum number of tiles covered by the carpet. This approach takes advantage of the sorted order of the tiles and moves the start of the carpet incrementally, updating the number of covered tiles using a two-pointer method.
188+
189+
#### Code in Different Languages
190+
191+
<Tabs>
192+
<TabItem value="C++" label="C++" default>
193+
<SolutionAuthor name="@AmruthaPariprolu"/>
194+
195+
```cpp
196+
#include <vector>
197+
#include <algorithm>
198+
#include <iostream>
199+
200+
int maxWhiteTilesOptimized(std::vector<std::vector<int>>& tiles, int carpetLen) {
201+
std::sort(tiles.begin(), tiles.end());
202+
int n = tiles.size();
203+
int maxCovered = 0;
204+
int currentCovered = 0;
205+
int j = 0;
206+
207+
for (int i = 0; i < n; ++i) {
208+
while (j < n && tiles[j][1] < tiles[i][0] + carpetLen) {
209+
currentCovered += tiles[j][1] - tiles[j][0] + 1;
210+
++j;
211+
}
212+
if (j < n) {
213+
maxCovered = std::max(maxCovered, currentCovered + std::max(0, tiles[i][0] + carpetLen - tiles[j][0]));
214+
} else {
215+
maxCovered = std::max(maxCovered, currentCovered);
216+
}
217+
currentCovered -= tiles[i][1] - tiles[i][0] + 1;
218+
}
219+
220+
return maxCovered;
221+
}
222+
223+
int main() {
224+
std::vector<std::vector<int>> tiles = {{1, 5}, {10, 11}, {12, 18}, {20, 25}, {30, 32}};
225+
int carpetLen = 10;
226+
std::cout << "Maximum white tiles covered: " << maxWhiteTilesOptimized(tiles, carpetLen) << std::endl;
227+
return 0;
228+
}
229+
230+
231+
```
232+
</TabItem>
233+
<TabItem value="Java" label="Java">
234+
<SolutionAuthor name="@AmruthaPariprolu"/>
235+
236+
```java
237+
import java.util.*;
238+
239+
public class Main {
240+
public static int maxWhiteTilesOptimized(int[][] tiles, int carpetLen) {
241+
Arrays.sort(tiles, Comparator.comparingInt(a -> a[0]));
242+
int n = tiles.length;
243+
int maxCovered = 0;
244+
int currentCovered = 0;
245+
int j = 0;
246+
247+
for (int i = 0; i < n; ++i) {
248+
while (j < n && tiles[j][1] < tiles[i][0] + carpetLen) {
249+
currentCovered += tiles[j][1] - tiles[j][0] + 1;
250+
++j;
251+
}
252+
if (j < n) {
253+
maxCovered = Math.max(maxCovered, currentCovered + Math.max(0, tiles[i][0] + carpetLen - tiles[j][0]));
254+
} else {
255+
maxCovered = Math.max(maxCovered, currentCovered);
256+
}
257+
currentCovered -= tiles[i][1] - tiles[i][0] + 1;
258+
}
259+
260+
return maxCovered;
261+
}
262+
263+
public static void main(String[] args) {
264+
int[][] tiles = {{1, 5}, {10, 11}, {12, 18}, {20, 25}, {30, 32}};
265+
int carpetLen = 10;
266+
System.out.println("Maximum white tiles covered: " + maxWhiteTilesOptimized(tiles, carpetLen));
267+
}
268+
}
269+
270+
271+
```
272+
273+
274+
</TabItem>
275+
<TabItem value="Python" label="Python">
276+
<SolutionAuthor name="@AmruthaPariprolu"/>
277+
278+
```python
279+
def max_white_tiles_optimized(tiles, carpet_len):
280+
tiles.sort()
281+
max_covered = 0
282+
current_covered = 0
283+
j = 0
284+
285+
for i in range(len(tiles)):
286+
while j < len(tiles) and tiles[j][1] < tiles[i][0] + carpet_len:
287+
current_covered += tiles[j][1] - tiles[j][0] + 1
288+
j += 1
289+
290+
if j < len(tiles):
291+
max_covered = max(max_covered, current_covered + max(0, tiles[i][0] + carpet_len - tiles[j][0]))
292+
else:
293+
max_covered = max(max_covered, current_covered)
294+
295+
current_covered -= tiles[i][1] - tiles[i][0] + 1
296+
297+
return max_covered
298+
299+
tiles = [[1, 5], [10, 11], [12, 18], [20, 25], [30, 32]]
300+
carpet_len = 10
301+
print("Maximum white tiles covered:", max_white_tiles_optimized(tiles, carpet_len))
302+
303+
304+
305+
```
306+
307+
</TabItem>
308+
</Tabs>
309+
310+
#### Complexity Analysis
311+
312+
- Time Complexity: $O(n*logn+n)$
313+
- because of sorting the tiles, followed by a linear scan with a sliding window.
314+
- Space Complexity: $O(1)$
315+
- as we only use a few extra variables for counting.
316+
- This approach is efficient and straightforward.
317+
318+
</tabItem>
319+
</Tabs>
320+
321+
---
322+
323+
<h2>Authors:</h2>
324+
325+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
326+
{['AmruthaPariprolu'].map(username => (
327+
<Author key={username} username={username} />
328+
))}
329+
</div>

0 commit comments

Comments
 (0)