Блог пользователя vaps2012000

Автор vaps2012000, история, 3 года назад, По-английски

how to find the next number which has all different digits and greater than the given number N? Like if N=2000 then the answer is 2013. how to approach this problem. where 1<=N<=10^16

  • Проголосовать: нравится
  • -18
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится +42 Проголосовать: не нравится

The answer will not exits if it has more than 10 digits. for less than or equal to 10 digits you can form all the permutations.

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

If you code in Python, you could use a dictionary that keeps adding the value TRUE for every digit of the number in consideration. After all the digits of the number have been traversed AND if a digit is found to already have a value TRUE in dictionary, clear the dictionary and move on to the next number. When a number with all digits distinct is found, break out of the loop.

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Could you please post a link to this problem? Thanks!

»
3 года назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

The question link u posted doesnt have such big limits on n..

»
3 года назад, # |
Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

Here's my greedy approach sorry abit messy that I believe will work for any number. The idea is:

  • ans cannot exceed 9876543210

  • start from left to right and when you find the digit that occurred before increase it, if it's 9 in increasing it will genereate a carry which which we have to add toward left.