MatchNumbersHard
SRM 311 · 2006-07-12 · by gevak
Problem Statement
Each digit can be represented using a certain number of matches. Your goal is to create the largest possible number using the matches that you have. For example, if you need 6 matches for zero, 7 matches for one, and 8 matches for two, and you have 21 matches, the largest number you can create is 210 (8 + 7 + 6 = 21 matches).
You are given a
Notes
- It is not necessary to use all given matches. Some matches may be left unused.
Constraints
- matches will contain between 1 and 10 elements, inclusive.
- Each element of matches will be an integer between 1 and 10 ^ 18 (fits in a long), inclusive, with no leading zeros.
- n will be an integer between 0 and 10 ^ 18 (fits in a long), inclusive, with no extra leading zeros.
{"6","7","8"}
"21"
Returns: {"3", "210", "210" }
Example from the problem statement.
{"1","7","8"}
"21"
Returns: {"15", "100000000000000", "100000000000000" }
{"1","1","1","1","1","1","1","1","1","1"}
"923372036854775807"
Returns: {"923372036854775807", "99999999999999999999999999999999999999999999999999", "99999999999999999999999999999999999999999999999999" }
A lot of nines.
{"1","923372036854775807"}
"923372036854775807"
Returns: {"1", "1", "1" }
{"1","923372036854775806"}
"923372036854775807"
Returns: {"2", "10", "10" }
{"1","923372036854775807"}
"923372036854775806"
Returns: {"1", "0", "0" }
Zero is the only digit that can be created. Note that no extra leading zeros should be added to any returned value.
{"1", "10"}
"1000000"
Returns: {"999991", "10000000000000000000000000000000000000000000000000", "00000000000000000000000000000000000000000000000000" }
There are 999990 zeros in the created number, so its last 50 digits are all zeros.
Submissions are judged against all 68 archived test cases, of which 7 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class MatchNumbersHard with a public method vector<string> maxNumber(vector<string> matches, string n) · 68 test cases · 2 s / 256 MB per case