Created
August 10, 2020 04:08
-
-
Save Irene-123/b33e9d7eb1cdd8da386012aaf3f86087 to your computer and use it in GitHub Desktop.
Monotone Increasing Digits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def monotoneIncreasingDigits(self, N: int) -> int: | |
n=N | |
if n==10: | |
return 9 | |
if n<10: | |
return n | |
arr=[int(i) for i in str(n)] | |
for i in range (len(arr)-1): | |
if arr[i]>arr[i+1]: | |
while i>0 and arr[i]<=arr[i-1]: | |
i-=1 | |
arr[i]-=1 | |
for j in range (i+1,len(arr)): | |
arr[j]=9 | |
res=int(''.join([str(k) for k in arr])) | |
return res | |
return n | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment