当前位置:网站首页>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;
}
};
边栏推荐
- FAQ | FAQ for building applications for large screen devices
- Automated defect analysis in electron microscopic images-论文阅读笔记
- lex && yacc && bison && flex 配置的问题
- Wechat applet obtains the information of an element (height, width, etc.) and converts PX to rpx.
- Vulkan-性能及精细化
- [shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
- 【雅思阅读】王希伟阅读P1(阅读判断题)
- Kubernetes simple introduction to writing YML
- File operation io-part2
- Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
猜你喜欢

深度剖析数据在内存中的存储
![[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)

【雅思阅读】王希伟阅读P1(阅读判断题)

1.11 - 总线

The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail

Two common methods and steps of character device registration

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

Pageoffice - bug modification journey

Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)

FAQ | FAQ for building applications for large screen devices
随机推荐
[Luogu p4320] road meets (round square tree)
Vulkan-实践第一弹
leetcode-871:最低加油次数
node_ Modules cannot be deleted
Meaning of Tencent cloud free SSL certificate extension file
【雅思阅读】王希伟阅读P2(阅读填空)
[daily training] 871 Minimum refueling times
Shell 实现文件基本操作(切割、排序、去重)
Machine learning: numpy version linear regression predicts Boston house prices
Nc50528 sliding window
JSON conversion tool class
Free we media essential tools sharing
Linux Software: how to install redis service
2022上半年值得被看见的10条文案,每一句都能带给你力量!
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力
Rust string slicing, structs, and enumeration classes
kubernetes编写yml简单入门
Briefly talk about other uses of operation and maintenance monitoring
Program analysis and Optimization - 9 appendix XLA buffer assignment