Skip to content

Commit

Permalink
Create monotone-increasing-digits.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Dec 3, 2017
1 parent 3eb4c3f commit 7dbc167
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Python/monotone-increasing-digits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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(xrange(1, len(nums))):
if nums[i-1] > nums[i]:
leftmost_inverted_idx = i
nums[i-1] -= 1
for i in xrange(leftmost_inverted_idx, len(nums)):
nums[i] = 9
return int("".join(map(str, nums)))

0 comments on commit 7dbc167

Please sign in to comment.