CableDonation
TCO08 Qual 3 · 2008-01-21 · by legakis
Problem Statement
A local charity is asking for donations of Ethernet cable. You realize that you probably have a lot of extra cable in your house, and make the decision that you will donate as much cable as you can spare.
You will be given a
If both the jth character of lengths[i] and the ith character of lengths[j] are greater than 0, this means that you have two cables connecting rooms i and j, and you can certainly donate at least one of them. If the ith character of lengths[i] is greater than 0, this indicates unused cable in room i, which you can donate without affecting your home network in any way.
You are not to rearrange any cables in your house; you are only to remove unnecessary ones. Return the maximum total length of cables (in meters) that you can donate. If any pair of rooms is not initially connected by some path, return -1.
Constraints
- lengths will contain between 1 and 50 elements, inclusive.
- The length of each element of lengths will be equal to the number of elements in lengths.
- Each character in lengths will be a letter ('a'-'z', 'A'-'Z') or '0' (zero).
{ "abc",
"def",
"ghi" }
Returns: 40
You can part with the following cables: length 1 in room 0 length 5 in room 1 length 9 in room 2 length 4 between rooms 0 and 1 length 7 between rooms 0 and 2 length 6 between rooms 1 and 2 length 8 between rooms 1 and 2 for a total of 40 meters.
{ "a0",
"0b" }
Returns: -1
The two rooms are not connected.
{ "0X00",
"00Y0",
"0000",
"00Z0" }
Returns: 0
You have no unnecessary cable.
{ "0000",
"b000",
"0b00",
"00b0" }
Returns: 0
{ "Az",
"aZ" }
Returns: 105
Submissions are judged against all 88 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class CableDonation with a public method int cable(vector<string> lengths) · 88 test cases · 2 s / 256 MB per case