We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.util.Arrays;importjava.util.Scanner;publicclassSolve_problems{staticvoidsolve(intn,int[]array){int[]tails=newint[n];intlen=1;tails[0]=array[0];for(inti=1;i<n;i++){if(array[i]<tails[0]){// New smallest valuetails[0]=array[i];}elseif(array[i]>tails[len-1]){// Array[i] extends the largest subsequencetails[len++]=array[i];}else{// Array[i] will become end candidate of an existing// subsequence or replace a candidate in the middleintpos=Arrays.binarySearch(tails,0,len,array[i]);if(pos<0){pos=-pos-1;}tails[pos]=array[i];}}System.out.println(len);}publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);// Inputintn=sc.nextInt();int[]array=newint[n];for(inti=0;i<n;i++)array[i]=sc.nextInt();// Solve the problemsolve(n,array);// Close the Scanner instancesc.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Longest Increasing Subsequence
You are viewing a single comment's thread. Return to all comments →