Java – Exam Problem – Largest 3 Rectangles + Some Eclipse Shortcuts

As you have probably noticed, this is probably my 5th or article, concerning Java Exam problems from the SoftUni. Today, I will take a deeper look into a problem, which is supposed to calculate the best sum of triangles. The problem description is available here. Before going into the solution, I simply want to put 6 eclipse shortcuts, which may be useful for those who struggle with this platform. The tricky thing in Eclipse is that you should put a BreakPoint, in order to start the step by step debug. It took me quite a lot of time to learn it:

Shortcut Key

Description

F5 Step Into the Function
F6 Next Step (as F8 in VBA)
F7 Step Out of the Function
F8 Go to next Breakpoint (as F5 in VBA)
ALT + Shift + D + J Debug Java Application
Ctrl+ Shift + F Auto Format

By the way, if you are interesting in shortcuts, this site is probably one of the best in the Internet. I have tried the Excel shortcuts and I really learned quite a few from there – check my advanced list.

Triangles

 

Back to the Java Problem – we have the following input and output requirements (copy + paste from the problem description):

Input

Output

Comments

[3×3][3×2][4×3][1×4][5×3][3×1] 31 4*3+1*4+5*3=31

What we need to do is to find a way to read the input line and to calculate the highest sum of consecutive triangles. Quite trivial, but the input seems to be problematic – we have three signs, which seem to be a little useless and still we need them to differentiate the triangles – ‘[‘ ‘x’ ‘]’.

As far as I was not able to solve the problem myself, I decided to take the original solution and to analyse it a little:

Here is what we do:

  • We read the first line;
  • We remove the empty spaces and the ‘[‘ sign from the string we have read;
  • We split the string using the ‘]’ as a divider;
  • We initialize an array ‘areas’, with the length of the splitted units of the array ‘rects’;
  • With the first for loop we fill the integer array areas with the result from the multiplication of the first side * second side;
  • Once we have this, it is easier – we have the numbers in array and we should select the three highest consecutive;
  • Due to the fact, that the minimum number of triangles is 3, it means that we should start our second for loop from 2;
  • Thus, we sum one by one, recording the highest result in the variable ‘max’;
  • Once the loop is over, we print the variable ‘max’;

So long!

About

VBA Developer

Tagged with: ,