当前位置:网站首页>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;
}
};
边栏推荐
- Workplace "digital people" don't eat or sleep 007 work system, can you "roll" them?
- hcip第六天笔记
- Error of Tencent cloud [100007] this env is not enable anonymous login
- @classmethod 装饰器
- Three ways of redis cluster
- [force deduction] 1030. Arrange matrix cells in distance order
- Preparing for the soft test for junior programmers in the second half of 2022
- Applet sharing function
- AQS of concurrent programming
- Turn off automatic update when brew executes commands
猜你喜欢

Amd epyc 9664 flagship specification exposure: 96 core 192 threads 480MB cache 3.8ghz frequency

Uncaught SyntaxError: Octal literals are not allowed in strict mode.

Three ways of redis cluster

Package management apt, dpkg

GCD details

What are the ranking strategies for mobile websites, independent apps and websites?

2022年下半年软考初级程序员备考

ES6 array de duplication new set()

Practice of online problem feedback module (13): realize multi parameter paging query list

Immortal software in the computer that I don't want to delete all my life
随机推荐
Preparing for the soft test for junior programmers in the second half of 2022
Application engineering safety monitoring of wireless vibrating wire acquisition instrument
Vscode plug-in development
MySQL 01: Source command
刷题-洛谷-P1146 硬币翻转
Hcip eighth day experiment
Amd epyc 9664 flagship specification exposure: 96 core 192 threads 480MB cache 3.8ghz frequency
Business analysis report and data visualization report of CDA level1 knowledge point summary
Three ways of redis cluster
Tm1637 four digit LED display module Arduino driver with second dot
Hcip seventh day notes
Brush questions - Luogu -p1150 Peter's smoke
What should I do if the high-level MySQL server cannot be installed and I forget the password (MySQL 8.0.29)?
Arduino code of key state machine for realizing single, double click, long press and other functions with esp32 timed interrupt
2022年下半年软考信息安全工程师如何备考?
应急科普|收好这份暑期安全指南,让孩子安全过暑假!
[原创]九点标定工具之机械手头部相机标定
Sports luxury or safety luxury? Which type of Asian Dragon and Volvo S60 should we start with?
Multidimensional pivoting analysis of CDA level1 knowledge points summary
Internal error of LabVIEW
