当前位置:网站首页>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;
}
};
边栏推荐
- mm中的GAN模型架构
- Array common operation methods sorting (including ES6) and detailed use
- Free we media essential tools sharing
- The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
- Shell 实现文件基本操作(切割、排序、去重)
- Wechat applet obtains the information of an element (height, width, etc.) and converts PX to rpx.
- Machine learning: numpy version linear regression predicts Boston house prices
- Introduction of UART, RS232, RS485, I2C and SPI
- Program analysis and Optimization - 9 appendix XLA buffer assignment
- 指针初阶(基础)
猜你喜欢
Automated defect analysis in electronic microscopic images
ftrace工具的介绍及使用
leetcode-849:到最近的人的最大距离
Detailed explanation of pod life cycle
Why is the website slow to open?
File operation io-part2
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
指针进阶(一)
Linux Software: how to install redis service
Multiprocess programming (II): Pipeline
随机推荐
百度智能云牵头打造智能云综合标准化平台
Teach you JDBC hand in hand -- structure separation
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
Shell implements basic file operations (cutting, sorting, and de duplication)
Sentry developer contribution Guide - configure pycharm
Use Jenkins II job
kubernetes编写yml简单入门
Redis21 classic interview questions, extreme pull interviewer
可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场
Multi process programming (III): message queue
Rust string slicing, structs, and enumeration classes
Hdu3507 (slope DP entry)
Briefly talk about other uses of operation and maintenance monitoring
There is an unknown problem in inserting data into the database
cordova-plugin-device获取设备信息插件导致华为审核不通过
[MCU project training] eight way answering machine
Kubernetes simple introduction to writing YML
Program analysis and Optimization - 9 appendix XLA buffer assignment
About the practice topic of screen related to unity screen, unity moves around a certain point inside
机器学习:numpy版本线性回归预测波士顿房价