Skip to content

Commit d5e635e

Browse files
authored
Create monotone_increasing_digits.py
1 parent 2fbe5bf commit d5e635e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

monotone_increasing_digits.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from functools import reduce
2+
3+
class Solution:
4+
"""
5+
@param num: a non-negative integer N
6+
@return: the largest number that is less than or equal to N with monotone increasing digits.
7+
"""
8+
def monotoneDigits(self, num):
9+
# write your code here
10+
digits = list(map(int, list(str(num))))
11+
digits_len = len(digits)
12+
j = digits_len
13+
for i in range(digits_len - 1, 0, -1):
14+
if digits[i] > digits[i - 1]:
15+
continue
16+
digits[i - 1] -= 1
17+
j = i
18+
for i in range(j, digits_len):
19+
digits[i] = 9
20+
return reduce(lambda x, y: x * 10 + y, digits)

0 commit comments

Comments
 (0)