-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path738.Monotone_Increasing_Digits.py
35 lines (33 loc) · 1.06 KB
/
738.Monotone_Increasing_Digits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import time
class Solution(object):
def monotoneIncreasingDigits(self, N):
"""
:type N: int
:rtype: int
"""
while not self.is_monotone(N):
#print("processing %d" % N)
#time.sleep(1)
n_str_arr = list(str(N))
prev = 0
for i in range(len(n_str_arr)):
num = int(n_str_arr[i])
if num < prev:
#print("befor: " + "\t".join(n_str_arr))
n_str_arr[i - 1] = str(prev - 1)
#print("after: " + "\t".join(n_str_arr))
for j in range(i, len(n_str_arr)):
n_str_arr[j] = "9"
#print("after: " + "\t".join(n_str_arr))
break
prev = num
N = int("".join(n_str_arr))
return N
def is_monotone(self, N):
n_str = str(N)
prev = "0"
for letter in n_str:
if int(letter) < int(prev):
return False
prev = letter
return True