# Subsequence with longest monotonic increasing

Source: Internet
Author: User

Subsequence with longest monotonic increasing

Question: design an O (N ^ 2) complexity algorithm to find the longest monotonic increasing subsequence of a sequence composed of N numbers.

`Import Java. util. extends; public class main {public static void main (string [] ARGs) {extends = new loads (system. in); While (partial. hasnext () {int n = seconds. nextint (); int [] Nums = new int [n + 1]; // number of series data int [] sub_len = new int [n + 1]; // sub_len [I] indicates the length of the longest ascending sequence from I to N int [] sub_index = new int [n + 1]; // sub_index [I] indicates the subscript for (INT I = 1; I <= N; I ++) {Nums [I] = numeric. nextint (); sub_l En [I] = 1; sub_index [I] = 0;} For (INT I = n-1; I> = 1; I --) {int max = 0, P = 0; // temporary variable for (Int J = I + 1; j <= N; j ++) {If (Nums [I] <Nums [J] & sub_len [J]> MAX) {max = sub_len [J]; P = J ;}if (P! = 0) {sub_len [I] = sub_len [p] + 1; sub_index [I] = P ;}} int max = 0, P = 0; for (INT I = 1; I <= N; I ++) {If (sub_len [I]> MAX) {max = sub_len [I]; P = I ;}} system. out. println ("maximum length:" + max); system. out. print ("Longest ascending sequence:"); While (P! = 0) {system. out. print (Nums [p] + ""); P = sub_index [p];} system. out. println ();} bytes. close ();}}`

Subsequence with longest monotonic increasing

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.