当前位置:网站首页>2271. Maximum number of white bricks covered by blanket ●●
2271. Maximum number of white bricks covered by blanket ●●
2022-07-25 13:53:00 【keep_ fan】
2271. The maximum number of white bricks covered by the blanket ●●
describe
Here's a two-dimensional array of integers tiles , among t i l e s [ i ] = [ l i , r i ] tiles[i] = [l_i, r_i] tiles[i]=[li,ri] , All in l i < = j < = r i l_i <= j <= r_i li<=j<=ri Each tile location between j All painted white .
I'll give you an integer at the same time carpetLen , Can be placed in Any location A blanket .
Please return to this blanket , most Sure How many tiles are covered .
Example
Input :tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10
Output :9
explain : Remove the blanket from the tile 10 Start placing .
Total coverage 9 A tile , So back 9 .
Note that there may be other schemes that can also cover 9 A tile .
It can be seen that , Tiles cannot cover more than 9 A tile .
Answer key
1. Double pointer + The sliding window
The core idea : The sliding window , The right pointer keeps expanding , Until you can't move back ( This paragraph is not completely covered ), Shrink the left pointer , Let the right pointer try to expand again .
Blanket coverage :
- Completely overwrite the current right paragraph , Record length , Right pointer moves right , Calculate the length of more coverage ;
- Covering part right paragraph , Record length , Blanket ( Left pointer ) Move right ;
- Do not overwrite the current right paragraph , Blanket ( Left pointer ) Move right .
- Time complexity : O ( n log n ) O(n\log n) O(nlogn), among n by tiles The length of . Yes tiles The time complexity of array sorting is O ( n log n ) O(n\log n) O(nlogn), The maximum time complexity of double pointer maintenance is O(n).
- Spatial complexity : O ( log n ) O(\log n) O(logn), That is, the stack space overhead of sorting .
class Solution {
public:
int maximumWhiteTiles(vector<vector<int>>& tiles, int carpetLen) {
sort(tiles.begin(), tiles.end(), [](vector<int>& a, vector<int>& b){
return a[0] < b[0]; // Sort in ascending order at the beginning
});
int n = tiles.size();
int left = 0, right = 0, len = 0, ans = 0;
while(left <= right && right < n){
int rightMax = tiles[left][0] + carpetLen - 1;
if(tiles[right][1] <= rightMax){
// Whole segment coverage
len += tiles[right][1] - tiles[right][0] + 1;
ans = max(ans, len);
++right; // When this section is completely covered , Then continue to move the right pointer , Otherwise, move the left pointer
}else{
// Incomplete segment coverage : This section partially covers / This paragraph does not cover
if(tiles[right][0] <= rightMax){
// Partial coverage
ans = max(ans, len + rightMax - tiles[right][0] + 1); // Because next, you have to move the left pointer , Therefore, directly compare the longest length under the current partial coverage
if(ans >= carpetLen) return carpetLen;
}
// Move the starting position of the blanket to the beginning of the next interval
len -= tiles[left][1] - tiles[left][0] + 1;
++left;
}
}
return ans;
}
};
边栏推荐
- 刷题-洛谷-P1075 质因数分解
- leetcode202---快乐数
- MySQL 01: Source command
- Working mode and sleep mode of nlm5 series wireless vibrating wire sensor acquisition instrument
- GCD details
- 刷题-洛谷-P1152 欢乐的跳
- Arduino code of key state machine for realizing single, double click, long press and other functions with esp32 timed interrupt
- Install oh my Zsh
- Azure Devops (XIV) use azure's private nuget warehouse
- [force buckle] 645. Wrong set
猜你喜欢

Applet starts wechat payment

2271. 毯子覆盖的最多白色砖块数 ●●

刷题-洛谷-P1161 开灯

刷题-洛谷-P1035 级数求和

刷题-洛谷-P1089 津津的储蓄计划

leetcode202---快乐数

Vscode plug-in development

Working mode and sleep mode of nlm5 series wireless vibrating wire sensor acquisition instrument

「数字安全」警惕 NFT的七大骗局

Construction and practice of yolov7 in hands-on teaching
随机推荐
Experiment the Arduino code of NTP network timing alarm clock with esp32+tm1638
运动豪华还是安全豪华?亚洲龙与沃尔沃S60该入手哪款?
Leetcode202 --- Happy number
Arduino code of key state machine for realizing single, double click, long press and other functions with esp32 timed interrupt
Working mode and sleep mode of nlm5 series wireless vibrating wire sensor acquisition instrument
hcip第十天笔记
MXNet对DenseNet(稠密连接网络)的实现
leetcode202---快乐数
Dr. Berkeley's "machine learning engineering" big truth; AI vice president '2022 ml job market' analysis; Large list of semiconductor start-ups; Large scale video face attribute data set; Cutting edge
Internal error of LabVIEW
Workplace "digital people" don't eat or sleep 007 work system, can you "roll" them?
Turn off automatic update when brew executes commands
"Digital security" alert NFT's seven Scams
Brush questions - Luogu -p1150 Peter's smoke
Sports luxury or safety luxury? Which type of Asian Dragon and Volvo S60 should we start with?
Install oh my Zsh
G027-op-ins-rhel-04 RedHat openstack creates a customized qcow2 format image
刷题-洛谷-P1146 硬币翻转
刷题-洛谷-P1150 Peter的烟
【Platform IO编译Hifive1-revB】*** [.pio\build\hifive1-revb\src\setupGPIO.o] Error 1的解决办法