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.
//package solve_problems;/* * 2024 ^_^ *ThinhNguyen97 * */importjava.math.BigInteger;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.Collections;importjava.util.Comparator;importjava.util.HashMap;importjava.util.Set;importjava.util.HashSet;importjava.util.LinkedList;importjava.util.List;importjava.util.Map;importjava.util.Queue;importjava.util.Scanner;importjava.util.Stack;publicclassSolve_problems{staticvoidsolve(intn,int[]array){List<Integer>res=newArrayList<>();res.add(array[0]);for(inti=1;i<n;i++){intpos=Collections.binarySearch(res,array[i]);if(pos<0){// Element not found, calculate the insertion pointpos=-pos-1;if(pos==res.size())res.add(array[i]);elseres.set(pos,array[i]);}// Print the current LIS// res.forEach((x) -> {// System.out.print(x + " ");// });// System.out.println();}// Print the length of the LISSystem.out.println(res.size());}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 →