Equal Stack problem solved out in JAVA
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n1 = in.nextInt();
int n2 = in.nextInt();
int n3 = in.nextInt();
int[] n={n1,n2,n3};
int[][] stack={new int[n1],new int[n2],new int[n3]};
int[] height={0,0,0};
for(int i=0;i<3;i++)
{
for(int j=0;j<n[i];j++)
{
int ch=in.nextInt();
stack[i][j]=ch;
height[i]+=ch;
}
}
int[] index={0,0,0};
int h=Math.min(Math.min(height[0],height[1]),height[2]);
while(height[0]!=height[1] || height[1]!=height[2])
{
for(int i=0;i<3;i++)
{
if(height[i]>h)
{
height[i]-=stack[i][index[i]++];
h=Math.min(h,height[i]);
}
}
}
System.out.println(h);
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n1 = in.nextInt();
int n2 = in.nextInt();
int n3 = in.nextInt();
int[] n={n1,n2,n3};
int[][] stack={new int[n1],new int[n2],new int[n3]};
int[] height={0,0,0};
for(int i=0;i<3;i++)
{
for(int j=0;j<n[i];j++)
{
int ch=in.nextInt();
stack[i][j]=ch;
height[i]+=ch;
}
}
int[] index={0,0,0};
int h=Math.min(Math.min(height[0],height[1]),height[2]);
while(height[0]!=height[1] || height[1]!=height[2])
{
for(int i=0;i<3;i++)
{
if(height[i]>h)
{
height[i]-=stack[i][index[i]++];
h=Math.min(h,height[i]);
}
}
}
System.out.println(h);
}
}
Comments
Post a Comment