题解 | #最长上升子序列(一)#
最长上升子序列(一)
https://www.nowcoder.com/practice/5164f38b67f846fb8699e9352695cd2f
using System;
using System.Collections.Generic;
using System.Linq;
class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 给定数组的最长严格上升子序列的长度。
* @param arr int整型一维数组 给定的数组
* @return int整型
*/
public int LIS (List<int> arr) {
int[] dp = new int[arr.Count];
if(arr.Count == 0) return 0;
dp[0] = 1;
for(int i = 1; i < arr.Count; i++){
int max = 1;
for(int j = 0; j < i; j++){
if(arr[i] > arr[j]) max = Math.Max(max, dp[j] + 1);
}
dp[i] = max;
}
return dp.Max();
}
}
查看5道真题和解析