First, this is a computational geometry problem, as is the second part.If this were a serious problem or had a huge amount of data, we wouldbe looking for a good data structure for holding a collection ofrectangles.�� We might consider quad-trees, k-d-trees, R-trees, ...With such a data structure, we might hope for O(n.log n) time wheren is the number of claims.
Your approach is O(sum(claim areas)**2).Since the sum of the claim areas is about half a million square inches,O(sum(claim areas)) would not be a problem, but squaring that mostdefinitely is.
The nested loops are not really a problem.
What bothers me a lot is your choice of data structures.Representing a point by (a asString , '*' , b asString) is, um,not a good idea.�� And I don't care if it is C, C++, C#, Java,Objective C, Eiffel, Ada, Fortran, Javascript, Ruby, Lisp, Smalltalk,or almost anything but AWK: STRINGS ARE WRONG.
Strings have their uses in input/output.�� Nietzsche wrote:���I teach you the Superman.�� Man is a thing to be overcome ���What is the ape to man?�� A jest or a thing of shame.�� Soshall man be to the Superman ��� a rope over the abyss.���
STRINGS ARE A THING TO BE OVERCOME.�� A string where you needhigh performance is a jest or a thing of shame.Alan J. Perlis wrote: 'The string is a stark data structureand everywhere it is passed there is much duplication ofprocess.�� It is a perfect vehicle for hiding information.'If you need to process information fast, get it *out* of stringform as early as possible, and back into string format foroutput as late as possible.
In this case, several programming languages already have abuilt-in data structure for points.In Smalltalk, it is called Point, and (a��@ b) will make one.When you need to represent a point, why not use a Point?When you need to represent a rectangle, why not use a Rectangle?
Moreover, there is something you do which is *scary*.�� You do aLINEAR SEARCH in the OrderedCollection "area".�� Again, this reallyhas very little to do with the programming language.�� You coulduse a Vector or an ArrayList in Java and the problem would be thesame.�� A few linear searches, or a lot of searches in a smallsequence, no worries.�� A lot of searches in a big sequence, OUCH.
Considering both parts of the problem, you need to be able todistinguish three different states for any given cell:��- no rectangle covers this cell��- one rectangle covers this cell��- two or more rectangles cover this cellWhat you want is some sort of two-dimensional sparse array mappingx,y coordinates to counts.�� One good answer is a hash table.Use map : Dictionary[Integer, Dictionary[Integer, Integer]]�� �� �� �� �� �� �� �� �� �� ��x�� �� �� �� �� �� �� �� �� ��y�� �� �� �� countread the claims as a Dictionary[Integer, Rectangle]�� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� id�� �� �� ��left@top extent: (width-1)@(height-1)create an empty Dictionary, map.for each rectangle in claims�� ��for each x from rectangle left to rectangle right�� �� �� row := map at: x ifAbsentPut: [Dictionary new].�� �� �� for each y from rectangle top to rectangle bottom�� �� �� �� ��increment (row at: y).let total be the sum over the map of "use #inject:into:"�� ��the number of cells in the row "use #count:"�� �� �� where the value in the cell is more than one.