TopCoder problem "Taxicab" used in SRM 213 (Division I Level One)

Problem Statement

    Taxicabs in New York City have a complicated fare structure:
  • $2.50 for the first unit or fraction thereof if there is less than one total unit.
  • $0.40 for each additional unit, where a unit is defined as:
    • 1/5 of a mile, when the taxicab is traveling at 6 miles an hour or more, or
    • 60 seconds, when the taxicab is traveling at less than 6 miles an hour.

You will be given the number of blocks the cab travels each minute and you are to calculate the total fare in dollars and cents. (There are 20 blocks in a mile.) You may assume that the taxi's speed is constant during each minute. Note that the number of units accumulated during a minute may not be an integer.

Return the total fare as a String (without a dollar sign), with exactly two digits to the right of the decimal point and no extraneous leading zeros.



Method signature:String calculateFare(int[] blocks)
(be sure your method is public)


-You should assume instant acceleration/deceleration between segments.
-There are 20 blocks in a mile.
-There are 100 cents in a dollar.


-blocks will contain between 1 and 50 elements, inclusive.
-Each element of blocks will be between 0 and 30, inclusive.


Returns: "2.50"
You didn't go anywhere, but you still get charged $2.50 for getting into the cab.
{4, 4}
Returns: "2.90"
You went four blocks (1/5 mile) the first minute, which cost you $2.50, and 4 blocks (another 1/5 mile) the second minute, which cost you $0.40 because your speed was 12 mph (1/5 mile / 1/60 hour)
{10, 2}
Returns: "3.30"
You go 10 blocks in the first minute. This is 2.5 fifths-of-a-mile; the first 1/5 mile costs $2.50 and the other 1.5 costs $0.60. The second minute, you go only 2 blocks, but it is still at least 6 mph. Two blocks is 0.5 fifths-of-a-mile, so you get charged another $0.20, for a total of $2.50 + $0.60 + $0.20 = $3.30.
Returns: "6.10"
Even though you went 1/2 mile (2.5 fifths of a mile), the cab never went more than 6 mph, so you get charged by-the-minute. $2.50 for the first minute and 40 cents for each additional minute brings you to $6.10.
Returns: "2.50"
Returns: "3.10"
Counterintuitively, the second minute costs less than the third, even though you go faster during the second minute.

Problem url:

Problem stats url:




PabloGilberto , lbackstrom , brett1479

Problem categories: