当前位置:网站首页>leetcode 300. Longest increasing subsequence
leetcode 300. Longest increasing subsequence
2022-06-12 06:21:00 【Well, let me see】
300. The longest increasing subsequence 
Thought analysis :
Maintain an array v, In this array v[i] Indicates that the longest subsequence length is i+1 The minimum value of the last bit of the subsequence of , And constantly updated , If the current number num > v[i], be v[i + 1] = num
Boundary situation 1: If the current number is larger than the largest of the array , Then the length should be updated , To add
Boundary situation 2: If the current number is smaller than the smallest of the array , Then the length remains the same , Update minimum
AC Code :
int lengthOfLIS(vector<int>& nums) {
vector<int> q;
for(auto x:nums) {
// Boundary situation 1: If the current number is larger than the largest of the array , Then the length should be updated , To add
if(q.empty() || x > q.back())
q.push_back(x);
// Boundary situation 2: If the current number is smaller than the smallest of the array , Then the length remains the same , Update minimum
else if(x <= q[0]) q[0] = x;
else {
// Binary search update the maintained array
int l = 0, r = q.size() - 1;
while(l < r) {
int mid = (l + r + 1) >> 1;
if(q[mid] < x) l = mid;
else r = mid - 1;
}
q[r + 1] = x;
}
}
return q.size();
}
边栏推荐
猜你喜欢

Why don't databases use hash tables?

RNN model
![How to increase heap size of JVM [duplicate] - how to increase heap size of JVM [duplicate]](/img/65/a214d137e230b1a1190feb03660f2c.jpg)
How to increase heap size of JVM [duplicate] - how to increase heap size of JVM [duplicate]

Tips for using the potplayer video player

SQL 注入读写文件

Redis problem (I) -- cache penetration, breakdown, avalanche

Opencv_ 100 questions_ Chapter V (21-25)

哈工大信息内容安全实验

Leetcode personal question solution (Sword finger offer3-5) 3 Duplicate number in array, 4 Find in 2D array, 5 Replace spaces

LeetCode个人题解(剑指offer3-5)3.数组中重复的数字,4.二维数组中的查找,5.替换空格
随机推荐
Touch screen setting for win7 system dual screen extended display
Directx11 advanced tutorial cluster based deffered shading
Simple spiral ladder generation for Houdini program modeling
Leetcode 第 80 場雙周賽題解
Textcnn (MR dataset - emotion classification)
Automatic modeling of Interchange
N-degree Bessel curve
Trunet: short videos generation from long videos via story preserving truncation (thesis translation)
Guns framework multi data source configuration without modifying the configuration file
sqlite交叉編譯動態庫
Zip and Items() difference
LeetCode-剑指Offer(第二版)个人题解完整版
Redis problem (I) -- cache penetration, breakdown, avalanche
SQLite cross compile dynamic library
Leetcode 第 80 场双周赛题解
English grammar_ Adverb_ With or without ly, the meaning is different
Mastering UI development with unity
Unity custom translucent surface material shader
摄像头拍摄运动物体,产生运动模糊/拖影的原因分析
Unity3d display FPS script