Trains the technique from
LeetCode 12Integer to RomanThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
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.
Example 1
2076 = 1000 + 1000 + 50 + 10 + 10 + 5 + 1, and no subtractive pair applies.
Example 2
1215 = 1000 + 100 + 100 + 10 + 5, so a thousand mark, two hundred marks, a ten mark and a five mark.
Example 3
1946 = 1000 + 900 + 40 + 5 + 1, where 900 is the pair RZ and 40 is the pair GN.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def stroke_code(amount: int) -> str:public String strokeCode(int amount)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.