Skip to content

Instantly share code, notes, and snippets.

@alekseytimoshchenko
Created May 25, 2019 12:33
Show Gist options
  • Save alekseytimoshchenko/463a39b115c878da0db53d67dc34d06e to your computer and use it in GitHub Desktop.
Save alekseytimoshchenko/463a39b115c878da0db53d67dc34d06e to your computer and use it in GitHub Desktop.
/**
* Method should find in closed range the smallest volume of box
*
* @param i start index
* @param j end index
*
* @return volume of the smallest box
*/
/*# Joni: bug caused an exception. see soluion below. */
// public int volumeOfSmallestBox(int i, int j)
// {
// int result = 0;
//
// if (_noOfBoxes >= j + 1)
// {
// result = _boxes[j].getVolume();
//
// for (int k = i ; k < j + 1 ; k++)
// {
// int boxVolume = _boxes[k].getVolume();
//
// if (boxVolume < result)
// {
// result = boxVolume;
// }
//
// }
// }
//
// return result;
// }
/*# Joni: solution */
public int volumeOfSmallestBox(int i, int j)
{
// make sure i and j are in boundaries
if (i < 0 || i >= _noOfBoxes || j < 0 || j >= _noOfBoxes)
{
return 0;
}
int h = 1, w = 1, l = 1;
// make sure j is bigger
if (i > j)
{
int temp = i;
i = j;
j = temp;
}
for ( ; i <= j ; i++)
{
if (_boxes[i].getLength() > l)
{
l = _boxes[i].getLength();
}
if (_boxes[i].getWidth() > w)
{
w = _boxes[i].getWidth();
}
if (_boxes[i].getHeight() > h)
{
h = _boxes[i].getHeight();
}
}
return (h + 1) * (l + 1) * (w + 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment