The problem
A bank will exchange a coin with the number n on it for three coins: n // 2, n // 3 and n // 4 (each divided and rounded down). You can also sell any coin for as many rupees as its number. You may exchange the new coins again, as often as you like. Return the most rupees you can end up with.
exchange(12) # 13: 12 -> 6, 4, 3; sell all three
exchange(11) # 11: 5 + 3 + 2 = 10, so sell it
exchange(24) # 27
Limits
nis from 0 to 10¹⁸ (a 1 followed by 18 zeros). A list with one entry for every number up tonwould never fit in memory.- The tests call your function many times in a row, with different values of n.
Builds on DYNP-2 Memoisation.
Continue on a laptop
Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.
Need a hint?0 of 3 opened
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
9 tests, not run yet
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | The example | Not run | |
| 2 | Zero | Not run | |
| 3 | Selling is better | Not run | |
| 4 | Twice | Not run | |
| 5 | Every n below 40 | Not run | |
| 6 | One million | Not run | |
| 7 | One billion | Not run | |
| 8 | 10¹² | Not run | |
| 9 | 10¹⁸ | Not run |