There are N buildings in a certain one-dimensional landscape. Each building has a height given by hi,i∈[1,N]. If you join K adjacent buildings, they will form a solid rectangle of area K×min(hi,hi+1,…,hi+k−1).
Given N buildings, find the greatest such solid area formed by consecutive buildings.
Input Format The first line contains N, the number of buildings altogether. The second line contains N space-separated integers, each representing the height of a building.
Output Format One integer representing the maximum area of rectangle formed.
Sample Input
5
1 2 3 4 5
Sample Output
9
This is my code , i tried to cross check it but there was a problem with it , whenever i access a sublist , it is getting sorted automatically and their positions are aso getting changed in the 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( i = c+1 ; i <= n ; i++ )
{
System.out.println(" value of c is "+c+" value of i is "+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 is "+x);
int y=x*(X.size());
System.out.println("projected value is "+y);
if(y > min)
min = y;
System.out.println("modified value is "+min);
}
}
System.out.println(min);
}
}
The subList() method is actually explicitly documented to give you a view of the original:
So subList() is not suitable if you want to modify it and not reflect the changes back to the original.
Instead make an explicit copy: