Skip to content

Instantly share code, notes, and snippets.

@kkuivi
Created April 3, 2017 01:11
Show Gist options
  • Save kkuivi/2fe79bcd3d08cc8e82ef3f51e203680a to your computer and use it in GitHub Desktop.
Save kkuivi/2fe79bcd3d08cc8e82ef3f51e203680a to your computer and use it in GitHub Desktop.
CodeFights: ShapeArea Solution
int shapeArea(int n) {
int numMiddleBoxes = 1 + (2 * (n-1));
int leftsideArea = 0;
int boxesToAdd = numMiddleBoxes-2;
while(boxesToAdd >= 1) {
leftsideArea += boxesToAdd;
boxesToAdd -= 2;
}
int area = (leftsideArea*2) + numMiddleBoxes;
return area;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment