Integer to Roman
Real-world version: Vault Stroke CodeThe statement here is our own: the same inputs, outputs and technique.
A vault labels every crate with a stroke code, a numeral written from seven marks:
| mark | value |
|---|---|
A | 1 |
E | 5 |
G | 10 |
N | 50 |
R | 100 |
W | 500 |
Z | 1000 |
A code is read left to right and its marks are added up, and clerks normally write the values in falling order, so NGGEA stands for 50 + 10 + 10 + 5 + 1 = 76.
To keep a mark from appearing four times in a row, six pairs may instead be written with the smaller mark in front of the larger one, which means subtract rather than add:
| pair | value |
|---|---|
AE | 4 |
AG | 9 |
GN | 40 |
GR | 90 |
RW | 400 |
RZ | 900 |
Given a positive integer amount, return the stroke code for it that uses the fewest marks. Exactly one code of that length exists for each amount in range.
Examples
Example 1
- Input
- amount = 2076
- Output
- "ZZNGGEA"
2076 = 1000 + 1000 + 50 + 10 + 10 + 5 + 1, and no subtractive pair applies.
Example 2
- Input
- amount = 1215
- Output
- "ZRRGE"
1215 = 1000 + 100 + 100 + 10 + 5, so a thousand mark, two hundred marks, a ten mark and a five mark.
Example 3
- Input
- amount = 1946
- Output
- "ZRZGNEA"
1946 = 1000 + 900 + 40 + 5 + 1, where 900 is the pair RZ and 40 is the pair GN.
Constraints
- 1 <= amount <= 3999
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def stroke_code(amount: int) -> str:public String strokeCode(int amount)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 14 test cases: 14 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Roman to IntegerReal-world version: Harborside Tally ValueEasy
- Fraction to Recurring DecimalReal-world version: Ratio Printed as a DecimalMedium
- Smallest Palindromic Rearrangement IIReal-world version: The k-th Mirrored RearrangementHard
- First Unique Character in a StringReal-world version: First Solo StopEasy