|
-
Jun 15th, 2005, 12:34 PM
#1
Thread Starter
Frenzied Member
Another Java Challenge
I found this site that has some really neat challenges.
http://www.devx.com/DevX/Door/17707
I'm stuck on the circle dance problem.(It says it's easy, but...)
I got a solution but it does't work for all the tests:
Code:
import java.util.*;
class CircleDance
{
public int arrangeDancers(int[] heights)
{
int[] lowestSoFar = heights;
Collections.reverse(Arrays.asList(heights));
if (heights.length == 3)
{
return heights[heights.length-1] - heights[0];
}
int maxDifference = heights[heights.length-1]-heights[0];
int temp;
int temp2;
for (int back=heights.length-1; back>=0; back--)
{
for(int front=0; front<heights.length; front++)
{
if (front == heights.length-1)
{
if (heights[front] > heights[0])
{
temp = heights[front];
temp2 = heights[0];
heights[front] = temp2;
heights[0] = temp;
}
}
else if (front < heights.length-1)
{
if (heights[front] > heights[front+1])
{
temp = heights[front];
temp2 = heights[front+1];
heights[front] = temp2;
heights[front+1] = temp;
for (int i=0; i<heights.length; i++)
{
if (i == heights.length-1)
{
if ((heights[i]+heights[0]) < maxDifference)
{
maxDifference = heights[i] + heights[0];
}
}
else if (i < heights.length-1)
{
if ((heights[i] + heights[i+1]) < maxDifference)
{
maxDifference = heights[i] + heights[i+1];
}
}
}
if ((heights[0] + heights[heights.length-1]) < maxDifference)
{
maxDifference = heights[0] + heights[heights.length-1];
}
}
}
}
}
return maxDifference;
}
public static void main(String[] args)
{
int[] d = {170,180,190};
CircleDance cd = new CircleDance();
System.out.println(cd.arrangeDancers(d));
}
}
Anyone up to the challenge?
-
Jun 15th, 2005, 12:37 PM
#2
Thread Starter
Frenzied Member
Re: Another Java Challenge
Wait, I got it. A lot of repeat code in mine
Code:
import java.util.*;
class CircleDance
{
public int arrangeDancers(int[] heights)
{
int[] lowestSoFar = heights;
Collections.reverse(Arrays.asList(heights));
if (heights.length == 3)
{
return heights[heights.length-1] - heights[0];
}
int maxDifference = heights[heights.length-1]-heights[0];
int temp;
int temp2;
for (int back=heights.length-1; back>=0; back--)
{
for(int front=0; front<heights.length; front++)
{
if (front == heights.length-1)
{
if (heights[front] > heights[0])
{
temp = heights[front];
temp2 = heights[0];
heights[front] = temp2;
heights[0] = temp;
for (int i=0; i<heights.length; i++)
{
if (i == heights.length-1)
{
if ((heights[i]+heights[0]) < maxDifference)
{
maxDifference = heights[i] + heights[0];
}
}
else if (i < heights.length-1)
{
if ((heights[i] + heights[i+1]) < maxDifference)
{
maxDifference = heights[i] + heights[i+1];
}
}
}
if ((heights[0] + heights[heights.length-1]) < maxDifference)
{
maxDifference = heights[0] + heights[heights.length-1];
}
}
}
else if (front < heights.length-1)
{
if (heights[front] > heights[front+1])
{
temp = heights[front];
temp2 = heights[front+1];
heights[front] = temp2;
heights[front+1] = temp;
for (int i=0; i<heights.length; i++)
{
if (i == heights.length-1)
{
if ((heights[i]+heights[0]) < maxDifference)
{
maxDifference = heights[i] + heights[0];
}
}
else if (i < heights.length-1)
{
if ((heights[i] + heights[i+1]) < maxDifference)
{
maxDifference = heights[i] + heights[i+1];
}
}
}
if ((heights[0] + heights[heights.length-1]) < maxDifference)
{
maxDifference = heights[0] + heights[heights.length-1];
}
}
}
}
}
return maxDifference;
}
public static void main(String[] args)
{
int[] d = {184,165,175,186,192,200,176,192,194,168,205,201};
CircleDance cd = new CircleDance();
System.out.println(cd.arrangeDancers(d));
}
}
Can anyone come up with a better solution?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|