We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2fbe5bf commit d5e635eCopy full SHA for d5e635e
monotone_increasing_digits.py
@@ -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