-
Notifications
You must be signed in to change notification settings - Fork 43
/
monotone-increasing-digits.py
228 lines (202 loc) · 6.19 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
738. Monotone Increasing Digits
Medium
An integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.
Given an integer n, return the largest number that is less than or equal to n with monotone increasing digits.
Example 1:
Input: n = 10
Output: 9
Example 2:
Input: n = 1234
Output: 1234
Example 3:
Input: n = 332
Output: 299
Constraints:
0 <= n <= 109
"""
# V0
class Solution:
def monotoneIncreasingDigits(self, N):
s = list(str(N));
### NOTICE HERE
for i in range(len(s) - 2,-1,-1):
# if int(s[i]) > int(s[i+1]) -> the string is not `monotone increase`
# -> we need to find the next biggest int,
# -> so we need to make all right hand side digit as '9'
# -> and minus current digit with 1 (s[i] = str(int(s[i]) - 1))
if int(s[i]) > int(s[i+1]):
### NOTICE HERE
for j in range(i+1,len(s)):
s[j] = '9'
s[i] = str(int(s[i]) - 1)
s = "".join(s)
return int(s)
# V0'
# brute force -> time out error
class Solution(object):
def monotoneIncreasingDigits(self, n):
def check(x):
x = str(x)
for i in range(len(x)-1,0, -1):
if int(x[i]) < int(x[i-1]):
return False
return True
while not check(n):
print ("n = " + str(n))
#print ("n = " + str(n) + "" + str(check(n)))
n = n - 1
#print ("n = " + str(n) + str(check(n)))
return n
# V0'
class Solution:
def monotoneIncreasingDigits(self, N):
s = str(N)
l = len(s)
res = 0
for i in range(len(s)):
if i == 0 or s[i] >= s[i-1]:
res += int(s[i]) * pow(10, l-1)
else:
return self.monotoneIncreasingDigits(res-1)
l -= 1
return res
# V1
# https://leetcode.com/problems/monotone-increasing-digits/discuss/666468/Python-O(n)-Solution-Easy-to-Understand
class Solution:
def monotoneIncreasingDigits(self, N: int) -> int:
s = list(str(N));
for i in range(len(s) - 2,-1,-1):
if int(s[i]) > int(s[i+1]):
for j in range(i+1,len(s)):
s[j] = '9'
s[i] = str(int(s[i]) - 1)
s = "".join(s)
return int(s)
### Test case : dev
# V1'
# http://bookshadow.com/weblog/2017/12/03/leetcode-monotone-increasing-digits/
class Solution(object):
def monotoneIncreasingDigits(self, N):
"""
:type N: int
:rtype: int
"""
sn = str(N)
size = len(sn)
flag = False
for x in range(size - 1):
if sn[x] > sn[x + 1]:
flag = True
break
if not flag: return N
while x > 0 and sn[x - 1] == sn[x]: x -= 1
y = len(sn) - x - 1
return (N // (10 ** y)) * (10 ** y) - 1
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/82721627
class Solution:
def monotoneIncreasingDigits(self, N):
"""
:type N: int
:rtype: int
"""
if N < 10: return N
num = [int(n) for n in str(N)[::-1]]
n = len(num)
ind = -1
for i in range(1, n):
if num[i] > num[i - 1] or (ind != -1 and num[i] == num[ind]):
ind = i
if ind == -1:
return N
res = '9' * ind + str(num[ind] - 1) + "".join(map(str, num[ind + 1:]))
return int(res[::-1])
# V1'''
# https://blog.csdn.net/fuxuemingzhu/article/details/82721627
class Solution:
def monotoneIncreasingDigits(self, N):
"""
:type N: int
:rtype: int
"""
if N < 10: return N
num = [int(n) for n in str(N)]
n = len(num)
ind = n - 1
for i in range(n - 2, -1, -1):
if num[i] > num[i + 1] or (ind != n - 1 and num[i] == num[ind]):
ind = i
if ind == n - 1:
return N
num[ind] -= 1
for i in range(ind + 1, n):
num[i] = 9
return int("".join(map(str, num)))
# V1''''
# https://leetcode.com/problems/monotone-increasing-digits/solution/
# IDEA : GREEDY
# Time Complexity: O(D^2)
# Space Complexity : O(D)
class Solution(object):
def monotoneIncreasingDigits(self, N):
digits = []
A = map(int, str(N))
for i in range(len(A)):
for d in range(1, 10):
if digits + [d] * (len(A)-i) > A:
digits.append(d-1)
break
else:
digits.append(9)
return int("".join(map(str, digits)))
# V1'''''
# https://leetcode.com/problems/monotone-increasing-digits/solution/
# IDEA : Truncate After Cliff
class Solution(object):
def monotoneIncreasingDigits(self, N):
S = list(str(N))
i = 1
while i < len(S) and S[i-1] <= S[i]:
i += 1
while 0 < i < len(S) and S[i-1] > S[i]:
S[i-1] = str(int(S[i-1]) - 1)
i -= 1
S[i+1:] = '9' * (len(S) - i-1)
return int("".join(S))
# V1''''''
# https://leetcode.com/problems/monotone-increasing-digits/discuss/181945/Fast-and-simple-40ms-Python-solution-using-recursion
class Solution:
def monotoneIncreasingDigits(self, N):
"""
:type N: int
:rtype: int
"""
s = str(N)
l = len(s)
res = 0
for i in range(len(s)):
if i == 0 or s[i] >= s[i-1]:
res += int(s[i]) * pow(10, l-1)
else:
return self.monotoneIncreasingDigits(res-1)
l -= 1
return res
# V2
# Time: O(logn) = O(1)
# Space: O(logn) = O(1)
class Solution(object):
def monotoneIncreasingDigits(self, N):
"""
:type N: int
:rtype: int
"""
nums = map(int, list(str(N)))
leftmost_inverted_idx = len(nums)
for i in reversed(range(1, len(nums))):
if nums[i-1] > nums[i]:
leftmost_inverted_idx = i
nums[i-1] -= 1
for i in range(leftmost_inverted_idx, len(nums)):
nums[i] = 9
return int("".join(map(str, nums)))