HJ103题解 | #Redraiment的走法#
Redraiment的走法
https://www.nowcoder.com/practice/24e6243b9f0446b081b1d6d32f2aa3aa
#include <iostream>
#include <vector>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int LengthOfLIS(vector<int>& nums) {
int maxSteps = 0;
int len = static_cast<int>(nums.size());
vector<int> dp;
// dp[i]为以第i个位置结尾的最长递增子序列的长度
for (int i = 0; i < len; i++) {
// 默认最小递增子序列长度为1
dp.push_back(1);
}
for (int i = 1; i < len; i++) {
for (int j = i - 1; j >= 0; j--) {
if (nums[j] < nums[i]) {
dp[i] = std::max(dp[j] + 1, dp[i]);
}
}
}
for (int i = 0; i < dp.size(); i++) {
maxSteps = std::max(maxSteps, dp[i]);
}
return maxSteps;
}
int main() {
int n;
vector<int> nums;
while (cin >> n) { // 注意 while 处理多个 case
for (int i = 0; i < n; i++) {
int num;
cin >> num;
nums.push_back(num);
}
std::cout << LengthOfLIS(nums) << std::endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")
海康威视公司福利 1407人发布
查看7道真题和解析