扫描二维码进入公司网站怎样做,代做关键词收录排名,建设一个网站需要几个角色,建网上商城的第三方网站哪个好牛客题霸 [ 最长递增子序列] C题解/答案
题目描述
给定数组arr#xff0c;设长度为n#xff0c;输出arr的最长递增子序列。#xff08;如果有多个答案#xff0c;请输出其中字典序最小的#xff09;
题意#xff1a;
直接暴力会超时 应该用二分贪心
题解#xff1a…牛客题霸 [ 最长递增子序列] C题解/答案
题目描述
给定数组arr设长度为n输出arr的最长递增子序列。如果有多个答案请输出其中字典序最小的
题意
直接暴力会超时 应该用二分贪心
题解
class Solution {
public:/*** retrun the longest increasing subsequence* param arr int整型vector the array* return int整型vector*/vectorint LIS(vectorint arr) {// write code hereint n arr.size();if (n 2) return arr;int ans 0;vectorint ret(n);vectorint st;for (int i 0; i n; i){if (st.size() 0 || arr[i] st.back()){st.push_back(arr[i]);ret[i] ans;}else{int l 0, h ans-1;while (l h){int m (lh)/2;if (st[m] arr[i]) l m1;else h m;}st[h] arr[i];ret[i] h1;}}vectorint res(ans);for (int i n-1; i 0; i--){if (ret[i] ans){res[--ans] arr[i];}}return res;}
};