I am finding for the effecient solution on that task
You are given K is summ of digits are in number and N is count of digits in the number.
So you have to find count of all numbers, the smallest number and the largest number.
Also It has to be sorted number. For example:
145 - sorted number, because 4 > 1, and 5 > 4.
382 - not sorted number, because 3 < 8, but 8 > 2.
Everything must correspond to the condition of the problem.
Here is what I did:
def find_all(sum_dig, digs):
min_num = 10000000000000000
max_num = -1
count = 0
for i in range((10 ** digs // 10), 10 ** digs):
summ = 0
for j in range(len(str(i))):
summ += int((str(i))[j])
if summ == sum_dig and str(i) == ''.join(sorted(str(i))):
count += 1
if i > max_num:
max_num = i
if i < min_num:
min_num = i
if count == 0:
return []
else:
return [count, min_num, max_num]
But that code works pretty slow, I get the error Execution Timed Out (12000 ms)
Could you tell is there any way to make it more effecient?
Any advices are appreciated!