Problem Statement
Coder rankings rise and fall; not everyone who gets into the "red" stays there. For the TopCoder web site, we need to draw a chart that shows the time and duration of red ranking for each coder, but we want to minimize the size of the chart. Our chart will have horizontal bars to show the span (series of consecutive TopCoder events) where each coder has a red ranking.
For example, we might have the following coders who are "red" for a series of SRMs indicated. These spans are inclusive.
- Coder "L" is red from SRM 97 to SRM 104, drops in ranking, and then is red again from SRM 110 to 119
- Coder "Z" is red from SRM 107 to SRM 110
- Coder "B" is red from SRM 100 to SRM 109
11111111111111111111
99900000000001111111111 SRM numbers (vertical)
78901234567890123456789
LLLLLLLL ZZZZ | Actual chart area; two lines, and SRM range
BBBBBBBBBBLLLLLLLLLL| of 97 to 119 gives 23 columns. Area = 2*23 = 46.
The length of the coder's handle does not impact how the graph is drawn. Single letter handles are used in this example for clarity; the desired answer would be the same no matter how long the handles were.
There are a few simple rules that govern where we can list a coder in the chart.
- If a coder is red for a consecutive set of events, from SRM x to SRM y, they must occupy a single horizontal row in the chart from x to y.
- A coder may have multiple spans in which they are red, but the spans do not need to occupy the same horizontal row ("L" above is an example of this). The spans for a coder must be separated by at least one SRM; thus, a span for coder "Z" from 107 to 110, and a second span from 111 to 115 will not be allowed by the constraints on the input.
- No two coders can overlap in any position, but they may be adjacent (as is shown for "B" and "L" in the example).
To minimize area, we need to minimize the number of rows in the chart. Create a class RedChart that contains a method area that takes a String[] coders; each element of coders contains a coder handle and the start and end SRM numbers of a span for which the coder is "red." The method returns an int representing the total area of the chart.
Definition
- Class:
- RedChart
- Method:
- area
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int area(String[] coders)
- (be sure your method is public)
Constraints
- coders will contain between 0 and 50 elements inclusive.
- Each element of coders will be at most 50 characters long.
- Each element of coders will consist of the alphanumeric coder handle ('a'-'z', 'A'-'Z', and '0'-'9' are the only allowed characters), followed by a single space, the SRM number for the start of a span, another single space, and the SRM number for the end of a span.
- Each coder handle will contain between 1 and 46 characters, inclusive.
- The SRM numbers will not have leading zeros.
- The SRM numbers will be integers between 1 and 1000000 (inclusive).
- The start of an SRM span will be less than or equal to the end of an SRM span.
- The spans for a coder must be separated by at least one SRM.
Examples
{"L 110 119", "Z 107 110", "B 100 109", "L 97 104"}Returns: 46
This is the illustrated example.{"A 1 10", "B 11 20", "C 21 30", "D 1 30"}Returns: 60
{"Abcdefg 1 3", "Abcdefg 5 6", "xyzzy 1 6"}Returns: 12
{}Returns: 0