Sublists of a list in java -


there n buildings in one-dimensional landscape. each building has height given hi,i∈[1,n]. if join k adjacent buildings, form solid rectangle of area k×min(hi,hi+1,…,hi+k−1).

given n buildings, find greatest such solid area formed consecutive buildings.

input format first line contains n, number of buildings altogether. second line contains n space-separated integers, each representing height of building.

output format 1 integer representing maximum area of rectangle formed.

sample input

5 1 2 3 4 5 

sample output

9 

this code , tried cross check there problem , whenever access sublist , getting sorted automatically , positions aso getting changed in original list

import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*;   public class solution  {  public static void main(string[] args)   {    scanner sc = new scanner(system.in);    int n=sc.nextint();     list<integer> al= new arraylist<integer>();      for(int i=0;i<n;i++)         al.add(sc.nextint());    int i, c,min=0;    for( c = 0 ; c < n ; c++ )   {       for( = c+1 ; <= n ; i++ )      {             system.out.println(" value of c "+c+" value of "+i);          list<integer> x = al.sublist(c,i);           for(int j=0;j<x.size();j++)              system.out.print(x.get(j)+" ");           system.out.println();           collections.sort(x);           for(int j=0;j<x.size();j++)              system.out.print(x.get(j)+" ");           system.out.println();           int x=x.get(0);          system.out.println("min value "+x);          int y=x*(x.size());          system.out.println("projected value "+y);           if(y > min)              min = y;           system.out.println("modified value "+min);        }    }      system.out.println(min);   } } 

the sublist() method explicitly documented give view of original:

returns view of portion of list between specified * {@code fromindex}, inclusive, , {@code toindex}, exclusive. (if * {@code fromindex} , {@code toindex} equal, returned list * empty.) returned list backed list, non-structural * changes in returned list reflected in list, , vice-versa. * returned list supports of optional list operations.

so sublist() not suitable if want modify , not reflect changes original.

instead make explicit copy:

list<integer> x = new arraylist<>(al.sublist(c,i)); 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -