java 求最長遞增子串行(可以不連續的情況)

2021-07-11 10:39:41 字數 922 閱讀 7949

題目大意:

given an unsorted array of integers, find the length of longest increasing subsequence.

for example,

given [10, 9, 2, 5, 3, 7, 101, 18],

the longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. note that there may be more than one lis combination, it is only necessary for you to return the length.

your algorithm should run in o(n2) complexity.

follow up: could you improve it to o(n log n) time complexity?

其中我的o(n2)的**如下:就是動態規劃,其中把最長路徑儲存了下來。。。

統計前面的數字的長度,然後後面的長度是(如果大於前面的數且長度+1大於原來的長度則更新現在的長度),**很清晰,如下:

package aaa;

public class main7 ;

find(n);

} public static void find(int a)

}} int max=0;

int index=0;

for(int i=0;imax)

} int length=max;

system.out.println(max+":"+index);

int r=new int[max];

for(int i=index;i>=0;i--)

} for(int i=0;i

LIS 求最長遞增子串行

動態規劃,dp 時間複雜度 olog n 2 include include include include include using namespace std int lis int a,int alen,int len if alen i maxlen return maxlen int m...

求最長單調遞增子串行

問題描述 設計乙個時間的演算法,找出由n個數組成的最長單調遞增子串行。演算法描述 演算法實現 includestruct date void ascorder struct date a,int n 輸出最長單調遞增子串行 找到最長單調遞增子串行開始的單元 int max 0 intfore for...

最長遞增子串行 Java 實現

1 n2 打表實現 初始化對角線為 1 對每乙個 i,遍歷 j 0 到 i 1 若a i a j 置 1。若a i a j 取第 j 行的最大值加 1。private static int getlargestlen int array for int i 1 i array.length i in...