当前位置:网站首页>Leetcode-1964: find the longest effective obstacle race route to each position
Leetcode-1964: find the longest effective obstacle race route to each position
2022-07-03 00:47:00 【Chrysanthemum headed bat】
leetcode-1964: Find the longest effective obstacle race route to each position
subject
Topic linking
You plan to build some obstacle race routes . To give you one Subscript from 0 Start Array of integers for obstacles , The array length is n , among obstacles[i] It means the first one i The height of obstacles .
For each between 0 and n - 1 Between ( contain 0 and n - 1) The subscript i , On the premise that the following conditions are met , Please find out obstacles The length of the longest obstacle route that can be formed :
- You can choose a subscript between 0 To i Between ( contain 0 and i) Any obstacle of .
- In this route , Must include section i There's a barrier .
- You must press the barrier in obstacles Medium The order of appearance Arrange these obstacles .
- Except for the first obstacle , The height of each obstacle in the route must be the same as the previous obstacle identical perhaps Higher .
Return length is n The answer array of ans , among ans[i] Is the subscript mentioned above i The length of the corresponding longest obstacle race route .
Example 1:
Input :obstacles = [1,2,3,2]
Output :[1,2,3,3]
explain : The longest effective obstacle route for each location is :
- i = 0: [1], [1] The length is 1
- i = 1: [1,2], [1,2] The length is 2
- i = 2: [1,2,3], [1,2,3] The length is 3
- i = 3: [1,2,3,2], [1,2,2] The length is 3
Example 2:
Input :obstacles = [2,2,1]
Output :[1,2,1]
explain : The longest effective obstacle route for each location is :
- i = 0: [2], [2] The length is 1
- i = 1: [2,2], [2,2] The length is 2
- i = 2: [2,2,1], [1] The length is 1
Example 3:
Input :obstacles = [3,1,5,6,4,2]
Output :[1,1,2,3,2,2]
explain : The longest effective obstacle route for each location is :
- i = 0: [3], [3] The length is 1
- i = 1: [3,1], [1] The length is 1
- i = 2: [3,1,5], [3,5] The length is 2, [1,5] It is also an effective obstacle race route
- i = 3: [3,1,5,6], [3,5,6] The length is 3, [1,5,6] It is also an effective obstacle race route
- i = 4: [3,1,5,6,4], [3,4] The length is 2, [1,4] It is also an effective obstacle race route
- i = 5: [3,1,5,6,4,2], [1,2] The length is 2
Problem solving
This question , At first glance, it's actually with leetcode-300: The longest increasing subsequence Same , But for this problem O ( n 2 ) O(n^2) O(n2) The method of will timeout , Therefore, we need a method with lower time complexity
Method 1 : Dynamic programming + Two points search
MinVals[i]
:i Indicates that the length of the current built is i The length of the obstacle route ,MinVals[i]
The length is i The height of the last obstacle of the obstacle length .
Then you can traverse the obstacles , And MinVals[i]
Just compare , If you are taller than him , So the result is MinVals[i]+1
. because Minvals[i]
Is a non strictly increasing array , So you can use binary search , Reduce time complexity to O ( n l o g 2 n ) O(nlog_2n) O(nlog2n)
class Solution {
public:
vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {
int n=obstacles.size();
vector<int> res;
vector<int> MinVals(n+1,INT_MAX);// structure i Minimum obstacle height value of length .
MinVals[0]=INT_MIN;
for(int i=0;i<n;i++){
// Two points search ( Find the longest , And the obstacle height is less than obstacles[i])
int l=0,r=i+1;//i+1 Is the maximum length that can be reached
while(l<r){
int mid=(l+r)/2;
if(obstacles[i]<MinVals[mid]) r=mid;
else l=mid+1;// Equal or equal to , Should look for a longer length
}// So the final result is l Is the length that has been built +1
res.push_back(l);
MinVals[l]=min(MinVals[l],obstacles[i]);
}
return res;
}
};
边栏推荐
- Is there a free text to speech tool to help recommend?
- Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t
- Nc20806 District interval
- Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
- Vulkan is not a "panacea"“
- kubernetes编写yml简单入门
- leetcode-224:基本计算器
- Some introduction and precautions about XML
- mm中的GAN模型架构
- Shell 实现文件基本操作(切割、排序、去重)
猜你喜欢
深度剖析数据在内存中的存储
Two common methods and steps of character device registration
Pageoffice - bug modification journey
百度智能云牵头打造智能云综合标准化平台
How to systematically learn machine learning
【AutoSAR 四 BSW概述】
【AutoSAR 三 RTE概述】
如何系统学习机器学习
Sentry developer contribution Guide - configure pycharm
University of Toronto:Anthony Coache | 深度强化学习的条件可诱导动态风险度量
随机推荐
2022上半年值得被看见的10条文案,每一句都能带给你力量!
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
lex && yacc && bison && flex 配置的问题
Set up nacos2 X cluster steps and problems encountered
1.12 - Instructions
【Pulsar文档】概念和架构/Concepts and Architecture
【AutoSAR 一 概述】
Some introduction and precautions about XML
Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
1.11 - bus
Automated defect analysis in electronic microscopic images
Is there a free text to speech tool to help recommend?
Vulkan practice first bullet
深度剖析数据在内存中的存储
Multiprocess programming (V): semaphores
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
Overlay of shutter (Pop-Up)
leetcode-871:最低加油次数
kubernetes编写yml简单入门