Let's trace num = 98368.
Digits: [9, 8, 3, 6, 8]. First, record the last index of each digit -.
Last occurrence map: appears last at index , at , at , at .
Scan from left. Index : digit . No digit larger than exists. Index : digit . No digit larger than exists to its right. Index : digit . Is there a digit - with a last index ? Yes, at index . Swap indices and : [9, 8, 8, 6, 3] = .
You scan each digit and for each check possible larger digits. That's where is the number of digits (). So time and space for bounded input.