LeetCode HOT100 - 最长递增子序列

LIS 比较经典的形式

一种相对较优的 DP 是长度为 len 的上升子序列,结尾最小 可以是多少

这是因为在长度固定的情况下,结尾越小,后续越有利

且显然,这样我们的 dp 数组是单调的

class Solution {
public:
    int lengthOfLIS(vector<int>& a) {
        vector<int> dp;
        for (auto x : a) {
            auto it = lower_bound(dp.begin(), dp.end(), x);
            if (it == dp.end()) {
                dp.emplace_back(x);
            } else {
                *it = x;
            }
        }
        return dp.size();
    }
};
posted @ 2026-04-02 00:07  rdcamelot  阅读(19)  评论(0)    收藏  举报