This is about as easy as it gets. It took longer to read the problem description than to actually code it up.
Nevertheless, there's still a few things worth noting.

Remember that we can use String.charAt(i) to get the character at a given position in the String. Also chars in java
are treated as 16-bit integers, so we can do normal integer arithmetic on them. The letters 'a' - 'z' are guaranteed to be
consecutive, as are 'A' - 'Z'. So an expression like 'c' - 'a' will yield a value of 2. In this problem, the actual ascii
values of the characters does not matter (A = 65 by the way).

To get the number of key presses for each letter we just subtract 'A' from that letter. But since, 'A' - 'A' would be 0,
we need to add 1 to each value.

The variable count is initialized to 0, and then is incremented by the value of each new letter. The operator +=
is just a sort cut for writing "count = count + ..."

Thank you for taking the time to read this solution. I welcome
any feedback you may have.

There's a lot going on in this problem. It's tough for division 2 level 2. The solution uses the Floyd-Warshall algorithm to determine
the shortest distance between all pairs of cities. If you're unfamiliar with the algorighm, check out this video tutorial.
Floyd-Warshall Tutorial Take the time to work through the example until you
understand what's going on.

One thing to be careful of when using this algorithm: you can't set unknown distances to Integer.MAX_VALUE. The reason is that we
need to add two distances together, and this will cause an overflow. To avoid this, I created INF_DISTANCE which is 1 greater than
any possible distance given the constraints of the problem.

When the section labeled Floyd-Warshall's algorithm complets, the distances[][] will contain a table that maps the distance from
any city to any other city. Then we just need to find the sets of cities that match the criteria, and determine the largest.

The number of possible subsets of all cities is reasonably small. Just 2^10 = 1024. So it's very possible to generate all
combinations and test each one. To generate all possible subsets, I used a trick using a bit mask. It's the first time a solution
on this site has used it, so I'll go into some detail as to how it works.

Imagine each set of cities is represented as a binary string. So "0000000111" would represent a set that included cities 0, 1, and 2. "1000000000"
is a set that includes only city 9, and "1111111111" contains all cities. By simply counting from 0 to 2^n (Where n is the number of cities/bits)
we'll get every possible combination of bits.
Note that care must be taken because the cities start at 1 and not 0. So city 9 is actually represented by the 10th bit from the right. city 0 is
in the first bit on the right.

Now, to tell if a given city is in the current set, we'll use an expression such as (citySetMask & (1 << city1) where citySetMask is the current binary
pattern, and city1 is the city that we're checking. The << operator taks the value of 1 (...001) and shifts it's bits to the left by the value of city1.
Let's say our citySetMask is "0001010111" and city1 is 4. The << operator will take 1 (0000000001) and shift if over 4 places (0000010000). We then
use the bit-wise and operator & to test the two values. In this case, the city is in the set.

The remainder of the logic if pretty straight forward. We initialize citySetDistance to -1. Then when the first other city is found, we'll set that
to the distance between those first two cities. allDistancesEqual is set to false if we ever find a pair of cities whose distances is not what
was previously set in citySetDistance. When allDistancesEqual becomes false, both the inner and outer while loops terminate, and we give up
on the current set. If all cities in the set do have equal distances, then we set max to the larger of max or cityCount.

Again, this is pretty tough for a division 2 level 2 problem. Compared to the early single round matches, like the 100 series, this would
be a division 1 level 2 or division 2 level 3 problem at least, but that appears to be the way things are going with these newer problem sets.

Thank you for taking the time to read this solution. I welcome
any feedback you may have.

First, I create a StringBuilder object out of the given String. This will make deleting characters from the string more efficient.
Every time a String is modified in Java, a new object is created. On the other hand, StringBuilder is mutable.

I used the boolean variable changed to note when any characters have been deleted. If we pass through the entire string with no changes,
then we're done. Note that I call sb.deleteCharAt(i) twice. The first deletes the first characters, and causes all subsequent characters to
shift one place to the left. The second call deletes the second of the double characters.

Upon exiting the while loop, simply check the size of the remaining string, and return Possible or Impossible accordingly.

Thank you for taking the time to read this solution. I welcome
any feedback you may have.

To solve this, I've created a class to represent the Master devices and another class to simulate the network. The transmitTime()
method then coordinates these objects and keeps track of how much time is needed to complete all the messages.

The Master class simply represents a device that wishes to send a message. It's initialized with the message, and the time it
requires to place each byte on the bus.

The Bus class represents the I2C System Management Bus. The idea is that after the bus is initialized or reset, Master objects can call setChar()
and setTime() to drop their request onto the bus. The bus will keep track of the smallest character, and the greatest time. It also remembers
all the characters in the current message.

When the transmitTime() method is called, it will create Master objects for each of the parameters. It adds all of these Master objects to
the incomplete Set. It will then loop until the incomplete Set is empty - all Master devices have completed sending their messages.

For each message, we check to see if the current message on the bus matches the complete message for any Master. In this case, that Master
is done, and is removed from the incomplete Set. Otherwise, each Master in the incomplete Set places it's character and time on the bus, so long as
it's message continues to match the bus's message. time is incremented, and the bus is set up to receive the next character.

After all Master objects have been removed from the incomplete Set, time will hold our finishing time.

Thank you for taking the time to read this solution. I welcome
any feedback you may have.

We need to consider two possibilities. First, that we can fold the paper length-wise and width-wise until it fits in box, and
second, that we must first rotate the paper, and then fold it down to fit into the box. We'll try both ways, and go with the
one that takes fewer folds.

On line 10, f1 is assigned the number of folds required if the maximum dimension of the paper is aligned with the maximum dimension
of the box. On the next line, f2 is assigned the number of folds if this were rotated 90°.

The countFolds() method simply counts how many times the box's dimension needs to be doubled before it can fit the paper. I choose to
double the size of the box rather than halving the size of the paper to avoid having to convert to doubles in order to deal with
fractions.

Thank you for taking the time to read this solution. I welcome
any feedback you may have.

The constraints of this problem make it small enough that a brute-force method works well. We could just
start at 1 and check to see if it is a Least Common Multiple (LCM) of each number in the range. If not,
just increment by 1 and try again. A simple optimization is to note that our answer must be a multiple
of last. So we can start our search there, and increment by the value of last to get the next number
to test.

The % operator is used to determine if one number divides another evenly. if ((a % b) == 0) then b
evenly divides a. Or, a is a multiple of b.

Note the break; statement in the inner-most while loop. This lets us quit on a potential LCM as soon as we
find a number that it is not a multiple of. When the break is encountered, i will be <= last, and the
if statement below will use this to determine if a LCM has been found or not.

Thank you for taking the time to read this solution. I welcome
any feedback you may have.

This is the sort of nasty little problem that you might encounter during a programming
interview. I've seen it asked with 3 burgers and a grill that holds two. That's easy
enough to explain. Coding it up for the general case is quite a bit trickier. It's
also very likely that you'll pass the given examples, and then fail during the challenge
phase, since it's so easy to make a mistake in the logic that goes undetected.

There are essentially five cases to think about:

hamburgers == 0. This is trivial, if there are no burgers, it takes no time

hamburgers <= panSize. Again, pretty trivial. We can put all burgers on the pan at once. Flip them after 5 minutes, and they'll be done in 10.
There's no way to shorten this.

(hamburgers % panSize) == 0. The number of burgers divides evenly by the panSize. Similar to the above case, but we repeat the process until
all hamburgers are gone.

Here's out special case. If the remainder of the burgers when divided by the pan size is <= half the pans size: (hamburgers % panSize) <= (panSize / 2),
then we can use a trick. We flip over half the burgers, set the other half of the burgers aside, and fill the pan with a new set of burgers.
This can save us the time it takes to cook one side of a burgers.

Failing the above cases, we fall back to the number of burgers divided by the size of the pan times how long it takes to cook a burger.

Thank you for taking the time to read this solution. I welcome
any feedback you may have.

The source and target tables map two inputs to a result. The result of a@b is source[a].charAt(b). Likewise, the result
of a~b is target[a].charAt(b). Since the value in the table is a char, we need to subtract '0' to get it's int value.

Now, just test that for all values of a and b. For any combination that fails the test, we'll add it to a List. Once complete, we
convert the List to an array, and return it.

Since we loop through a in the outer loop, and then b in the inner loop, entries will be added to the results List in exactly the order
we need. There is no need to sort them to match the expected output.

Thank you for taking the time to read this solution. I welcome
any feedback you may have.

Not much difficulty here. avgMinutes() loops through all the finishing times and keeps a running total of the number of minutes for all the competitors
to finish. Then it just divides the today by the number of times - casting to double to avoid losing decimal places. To round off, just add 0.5 and
truncate - a trick everyone should know. There's no danger of overflow here, so just stick with the simplest thing that works.

Calculating the number of minutes for a given time is also pretty simple. We just need to take care dealing with the 12:00 PM and 12:00 AM hours. Here,
if it's 12 PM, I set the hours to 0. If it's 12 AM, I set hours to 24, and subtract a day. Then for any PM times, I add 12 hours. It may take a minute
to think it through, but it's not tough. Remember to subtract off the 8:00 AM starting time. Then it's just a matter of adding up the number of minutes
in a day, an hour, and the minutes portion.

Thank you for taking the time to read this solution. I welcome
any feedback you may have.

The solution uses recursion to generate all valid combinations of cross-court "c", and down-the-line "d" shots. The howMany() method simply resets the counter
variable numPossible, and the launches the recursive method addShots(). When addShots() completes, we'll have our answer in numPossible.

It's important to note the constraints given by the problem. numLength can be at most 18. Since we have only two characters (c and d), there can be at most
2^18 = 262,144 possible sequences. This is entirely possible to solve using a brute-force algorithm. If there were one or two more shot types, say a passing shot
and/or a lob, then we'd be looking at 3^18 = 387,420,489 and 4^18 = ~69 billion which would make brute-force less feasible.

I've left in two variations of the addShots() method: addShotsEarlyValidate() and addShotsDelayedValidate(). In addShotsEarlyValidate(), we check each String as it
is being built to ensure it will be valid before making the recursive call. The idea is to limit the size of the recursive tree. However, this results in many
more calls to isValid(). Alternatively, addShotsDelayedValidate() allows all possible patterns to continue, and only calls isValid() once the desired length has been
reached. This results in a larger recursive tree, but limits the number of calls to isValid(). I ran both against the provided test cases, and
addShotsDelayedValidate() ran in roughly half as much time.

The isValid() method simply calls countMatches() on each string in the forbidden array. It exits as soon as the value of allowed is reached.

The real work is performed in countMatches(). countMatches() works left to right through the str String. At each character, it checks to see if pat
can be found anywhere to the right of the current index. It too keeps track of the number of matches it has found and exits early once maxMatches is reached.

Thank you for taking the time to read this solution. I welcome
any feedback you may have.

The main method buildTrain() simply sets the initial value of the train and then loops through each of the remaining cars, calling processCar() on them.
All of the hard work is done inside processCar().
After all the cars have been processed, we note the length of the train, and then call removeAllButLastChar() before returning the results.

processCar() works by moving through the train string from beginning to end. The variable trainIdx is the current character in the train string that we're examining.
carIdx is an index into the car String, and startOfSuffix will hold the position of the beginning of a possible prefix/suffix match. This is in case the match
ultimately fails so we can pick up from the next character.

First, processCar() skips over characters in train until it finds one that matches the first
character of car. If there are no matches before reaching the end of train, then just return train. Assuming that the first character of car appears
in train, we mark that location as the beginning of a possible suffix. Then we walk through both strings one character at a time until we either reach
the end of one of the Strings, or until the characters no longer match.

If we reach the end of the train string, then we have a possible prefix/suffix match. Check to make sure that our match does not include the entire
train or car String, as this would not be a "proper" prefix or suffix. If that test passes, then we construct the result string by appending the remaining
characters from car onto the end of train.

If the characters cease to match up, or if we reach the end of the car string, then we reset carIdx back to the beginning of the car String and
set the trainIdx to the character following where we began the previous prefix/suffix match attempt. We then continue trying to find a match
from that point.

With the hard part done, we now turn to removeAllButLastChar(). First we create a Set of characters to hold all of the characters we've seen so far.
Then there's a StringBuilder to build up our result.
Next, we'll work backward through the string. For each chacter, if it exists in the usedChars Set, then skip it. If it's not in that set, add it, and
then add that character to the front of our StringBuilder.

Thank you for taking the time to read this solution. I welcome
any feedback you may have.

The quilt is represented as a 2 dimensional array of ints, where the value of each location is an index into the colorList array. We start by initializing
each location in the quilt to -1 to indicate that the color has not yet been choosen.

The next step is to determine where the first patch will be located. Here it might help to draw some examples out on paper. You'll find that the origin
works out to x = width / 2 and y = length / 2. Remember, the / operator removes any decimal portion of the division, effectively rounding down.

To determine the color of the first patch, we simply implement the 3 rules that were given. This is found in getPatchColor(). getLeastNeighbor() looks
at the 9 positions surrounding the current location, and counts the number of times each color appears. Here we have to be careful not to look outside
the bounds of the arrays, and to ignore any place where the color has not been set. Once we've counted how many times each color appears, we find the
smallest number, and finally return a list that contains all colors whose count is equal to that smallest number.

If the list size is 1, we're done. If there are ties, we move on to rule 2. The array colorUsageQuilt keeps track of how many times each color
is used in the entire quilt. We use this and similar logic as before to return the colors that came out of rule 1 which now match the minimum count. If there
are still ties, we just return the color with the smallest index.

That takes care of determining the color for a patch. The next step is to devise a method to move from one patch to the next following the outward spiraling pattern.
Here the arrays spiralX and spiralY come into play and the related variables diretion, lineLength, and lineLengthMax. direction provides an index into spiralX
and spiralY to get the change in x and y to move to the next location. We also keep track of the current line length, and the maximum length for the current line.
This tells us when to change direction.

Initially the direction is to the right, and the lineLengthMax is 1. Then we set the color of the first patch, and lineLength becomes 1. This matches
lineLengthMax, so we increment direction (mod 4) which corresponds to up, and reset lineLength. Whenever the direction changes to up or down, we increment
lineLengthMax. This setup gives us a nice counter-clockwise outward spiral movement.

Finally, we just need to loop through the number of patches, setting the color at each, and then moving on to the next patch. After all the patches have
been set, we need to back up one place since we'll have moved on to the next location. Then just return the color there. Remember, the value at that location
is an index into colorList.

Thank you for taking the time to read this solution. I welcome
any feedback you may have.

In my first attempt at this problem, I built a regular expression out of each of the codes and then simply checked to see if the key presses were matched
by that regular expression. I inserted a '+' after each character of the code, meaning that it matches on 1 or more characters from key presses. So, if
the code was "ABC", the regular expression became "A+B+C+" which would match "AAAAAAAAABCCC". This worked great, except that there are some test cases
that timed out. For example, checking to see if "A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+A+"
matches "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" is extermely slow. Regular expression matching in Java can run in O(n^2) time, making this
approach unfeasible. For a good explanation of Regular Expression running time, see Regular Expression
Matching Can Be Simple And Fast

This solution encodes the key presses and all of the codes by replacing repeating characters (or even a single character) with a character and it's count.
The key press sequence "AAAAAAAAAA" is encoded as {A,10}. The character and it's cound are bound together using a CharAndCount class.

Once the Stirngs are encoded, it becomes much easier to see if there is a match. We'll use two indexes into the key press list: pStartIdx, and pCurrentIdx.
pStartIdx begins at the first character of the key press string and slowly makes its way toward the end. For each position of pStartIdx, pCurrentIdx searches
the remainder of the key press string, beginning at pStartIdx and going to the end attempting to match the given code. If no match is found, then pStartIdx
is incremented and the process repeats.

In order for the code the match, there must be a sequence of characters in presses that matches the sequence given by code. Also, the count in presses must be
greater than, or equal to, the count in code for each corresponding character.

Thank you for taking the time to read this solution. I welcome
any feedback you may have.

To solve this, we first need to assign ranks based on each judge's score, and then sort the contestants based on their rankings. To make things easy, a
Contestant class is used to store all the data related to a single contestant, and two Comparator classes help out with the sorting.

The constructor for the Contestant class handles reading in a string and parsing out the name and the scores. The name is the first element, and the
remainder of the String is chopped up into ints and copied to the scores array. Note that at this point I strip out the decimal place. I'll add that back
in when displaying the results. But for now, it'll be easier to effectively just multiply all scores by 10. Finally, the constructor initializes an array to
hold the rankings, but this cannot be filled in yet.

The sortResults() method begins by first checking to ensure that the data parameter is not empty, and then creates a List to store the Contestants.
Then, for each judge,
we'll create a new ContestantComparatorByJudge comparator to help sort the contestants by the current judge. The ContestantComparatorByJudge class takes
a int in it's constructor, and uses that to compare the scores in the Contestan's score array. Note that the compare() method multiplies the result of
Integer.compare() by -1 because we want the contestants in order from highest to lowest. Once the contestants are sorted by a given judge's score, we
can assign a rank to each contestant for that judge. Here, we need to be careful in case there is a tie in the score.

Once all the ranks are assigned, a different comparator, ContestantComparatorOverall, is used to sort the contestants one last time. ContestantComparatorOverall
first compares by the sum of all ranks (lowest to highest), then if there is a tie, by total score (highest to lowest). If there is still a tie, it
compares their names.

After the contestans are sorted in their final order, just loop through them, using their toString() method to build the String[] result.

All in all, this was not too hard for a 1000 point problem. Just a few key points:

If possible, convert decimals to integers to keep things easy.

Use Collections.sort to do all the sorting. Just implement the custom comparators.

Be careful with how you handle ties.

If you can handle that, there's really not much to this problem.

Thank you for taking the time to read this solution. I welcome
any feedback you may have.

To solve this, we need to determine the length of each side of the inscribed polygon.

To construct the inscribed polygon, start by imagining spokes coming from the center of the circle out to the
edge. The number of spokes equals the number of sides in the polygon. The polygon will be drawn by
connecting the points where the spokes intersect the circle.

Next, the angle between the spokes is simply 360° divided by the number of spokes (sides). I've called
this innerAngle.

outerAngle is the angle formed between a spoke and a side of the inscribed polygon. Since two adjacent spokes
and the connecting edge between them constitute a triangle, the sum of their interior angles is 180° Therefore,
outerAngle is 180° - innerAngle divided by 2.

Now, the length of the side of the polygon is just the cosine of the outerAngle. Note that Math.cos()
expects a value in radians, so remember to call Math.toRadians() first since we've been working in degrees.

Finally, just return numSides * lengthOfSide.

Be careful to cast to a double where necessary in order to avoid losing precision. You will not get the
correct answer if the cast is left out of the calculation for the innerAngle.

If you need to brush up on your trigonometry, there are plenty of sites to help out. I recommend checking out
the Kahn Academy

As a final note, the TopCoder editorials given an even shorter answer:

return numSides * Math.sin(Math.PI / numSides);

I'm not sure how I feel about using Math.PI when we're trying to approximate Π but I supposed that's being
picky. Besides, I'm sure the toRadians() method uses Π anyway.

Thank you for taking the time to read this solution. I welcome
any feedback you may have.

Rather than deal the first card to player one, then the next card to player two, and so on; this solution determines all of the cards
that will end up in a player's hand before moving on to the next player.

It's simple enough to determine how many cards each player will get, just divide the length of the deck by the number of players. The division
operator rounds down, which ensures that all players get the same number of cards without needing more cards than there are in the deck.

The outer-most for loop loops through each of the players. For each player, their first card will be in position i. Then we just add
each card that is numPlayers further down in the deck until the player has the correct number of cards. Each card is added to a StringBuffer, which
gets converted into a String and added to the appropriate place in the hands[] array.

Thank you for taking the time to read this solution. I welcome
any feedback you may have.