DEV Community

loading...

Discussion on: Daily Challenge #133 - Suitcase Packing

Collapse
andreasjakof profile image
Andreas Jakof

The long side is the obvious case, but what if

  • Square A: 1x1
  • Square B: 2x2
  • Rectangle 3x1

=> FitIn(1,2,3,1)?

in C#

public bool FitIn(int a, int b, int m, int n)
{
   //both squares (added) must fit into the long side of the rectangle AND
   //the bigger single square must fit into the short side of the rectangle

   int longSide = Math.Max(m,n);
   int shortSide = Math.Min(m,n);
   int addedSquareLengths = a + b;
   int biggerSingleSquareSize = Math.Max(a,b);

   //do they fit?
   bool fitsLong = addedSquareLengths <= longSide;
   bool fitsShort = biggerSingleSquareSize <= shortSide;

   return fitsLong && fitsShort;
}

I know, it's a bit verbose, but very self explanatory. A short version will look like this:

public bool FitIn(int a, int b, int m, int n) =>
   (a + b <= Math.Max(m,n)) && //longSide
   (Math.Max(a,b) <= Math.Min(m,n)); //shortSide