Showing posts with label csharp. Show all posts
Showing posts with label csharp. Show all posts

Wednesday, July 16, 2008

TopCoder - SRM 145 - Div 2

The following problems are from Single Round Match No. 145, Division 2 of TopCoder's Algorithm Competition. (Full problem specifications are copyright TopCoder; and, can be found in the TopCoder problem archives.)

250 Point Problem
Write a method count that, given a matrix and a set of fill values, returns the number of elements in the matrix that make up the fill value set.
Solution.
Unit Tests For Sample Data.

I found the original problem statement for this problem to be very ambiguous. The actual context of the problem is image dithering; the given matrix represents a colored display, and the set of fill values represents dithering colors. The solution objective is to count the number of pixels in the display that are part of a dithered color. The ambiguity is in how to treat a single pixel that is colored with one of the dithering colors, but is surrounded by pixels which are not colored by dithering colors. The sample data for the problems hints that such a pixel should be counted, while my (very basic) understanding of image dithering suggests such a pixel should not be counted. In the end, I designed my algorithm to do the latter.

500 Point Problem
Write a method getPercentages that, given a time quantity in HH:MM:SS format, returns the number of times a whole percentage would be encountered if a timer were to count up and, each second, compute the ratio of elapsed time to total time. This count of whole percentages should exclude 0% and 100%.
Solution.
Unit Tests For Sample Data.

From a different perspective, this problem is actually asking how many pieces (minus one) a quantity can be divided into, with the stipulations that each piece must represent a whole percentage of the quantity, and that each piece must consist of a whole number of units. Because of the first stipulation, the maximum number of pieces we are interested in is 100. Thus, a simple, brute force solution which tests the two stipulations on 100 pieces, 99 pieces, 98 pieces, etc, works well.

1100 Point Problem
Write a method motorUse which simulates a motorized vending machine and, when given list of purchases, returns the number of seconds the motor is active.
Solution.
Unit Tests For Sample Data.

This problem was different, in the algorithmic sense, than other TopCoder problems I've seen. The difficulty in it came mostly from having to adhere to the many details in the problem specification. Consequently, the problem seemed more a software engineering puzzle; but, it was still a fun change of pace.

Sunday, July 6, 2008

TopCoder - SRM 144 - Div 2 - 1100 Pt

From Single Round Match No. 144 of TopCoder's Algorithm Competition:
Write a method estimateTimeOut that, given an edge-weighted tree, returns the minimum cost to visit all nodes. [Plus some backstory about a power outage, underground transformers, and a catacombic sewer.]
(Full problem specification is copyright TopCoder; and, can be found in the TopCoder problem archives.)

Solution.
Unit Tests For Sample Data.

This problem was used as the Division 2 1100-point problem. It reminds me of certain ACM-ICPC problems: mix two parts strong underlying data structure and algorithm with one part playful, semi-verbose problem description.

The solution to the problem is to realize that each edge in the tree will need to be traversed twice (once to go to a node, and once to come back from it) in order to visit all nodes, except for the edges on the path to the last visited node. Therefore, the minimum cost can be calculated by finding the most expensive node to reach (from the tree's root), and subtracting the cost reaching that node from twice the sum of all edge weights.

Wednesday, June 25, 2008

TopCoder - SRM 144 - Div 2 - 550 Pt

From Single Round Match No. 144 of TopCoder's Algorithm Competition:
Write a method decode that, given a string which represents an encoded binary string, where the encoding method dictates bitenc[i] = bitorig[i-1] + bitorig[i] + bitorig[i+1], returns a string[] containing the two possible original binary strings. These two possible originals are yielded by assuming either bitorig[0] == 0 or bitorig[0] == 1. If the encoded string cannot be produced via an assumption, return "NONE" in place of a binary string.
(Full problem specification is copyright TopCoder; and, can be found in the TopCoder problem archives.)

Solution.
Unit Tests For Sample Data.

This problem was used as the Division 2 550-point problem and as the Division 1 300-point problem. The solution here is to rearrange bitenc[i] = bitorig[i-1] + bitorig[i] + bitorig[i+1] to yield bitorig[j] = bitenc[j-1] - bitorig[j-1] - bitorig[j-2]. Then once we generate our original, we only have to verify that bitenc[ilast] == bitorig[ilast-1] + bitorig[ilast] (and that the original contains only binary digits).

Sunday, June 15, 2008

TopCoder - SRM 144 - Div 2 - 200 Pt

From Single Round Match No. 144 of TopCoder's Algorithm Competition:
Write a method whatTime which takes an int representing the number of seconds since midnight on some day, and returns a string formatted as H:M:S.
(Full problem specification is copyright TopCoder; and, can be found in the TopCoder problem archives.)

Solution.
Unit Tests For Sample Data.

This problem was used as the Division 2 200-point problem. Being a 200-point problem, it's very straightforward; and, there are no real "gotchas" involved in the solution.