Solution
Recall the algorithm for this problem that works for single-digit numbers.
:
empty string
while is not empty:
-
for each in :
if :
append from
remove from
return
As we already know, this algorithm does not always maximize your salary. For example, for an input consisting of two integers, and , it returns , while the largest number is .
Not to worry, all you need to do to maximize your salary is replace the line
if ≥ :
with the following line
if :
for an appropriately implemented function IsBetter
. For example, IsBetter(3, 23)
should return True
.
Stop and think: How would you implement
IsBetter
?
Code
Here is the code for the algorithm discussed above. Click the “Run” button to see how it works.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.