#4054. [GESP202503 七级 C++] 第 11 题

[GESP202503 七级 C++] 第 11 题

给定一个整数数组 nums,找到其中最长的严格上升子序列的长度。 子序列是指从原数组中删除一些元素(或不删除)后,剩余元素保持原有顺序的序列。 该程序的时间复杂度为( )

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int lengthOfLIS(vector<int>& nums) {
    int n = nums.size();
    if (n == 0) return 0;
    vector<int> dp(n, 1);

    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            if (nums[i] > nums[j]) {
                _________________________
            }
        }
    }
    return *max_element(dp.begin(), dp.end());
}

int main() {
    int n;
    cin >> n;
    vector<int> nums(n);
    for (int i = 0; i < n; i++) {
        cin >> nums[i];
    }

    int result = lengthOfLIS(nums);
    cout << result << endl;

    return 0;
}

{{ select(1) }}

  • O(n2)O(n^2)
  • O(n)O(n)
  • O(log(n))O(\log(n))
  • O(nlog(n))O(n\log(n))