Advertisement

Monotone Increasing Digits - LeetCode 738 Solution

Monotone Increasing Digits - Complete Solution Guide

Monotone Increasing Digits is LeetCode problem 738, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 <= 10 9

Detailed Explanation

The problem asks us to find the largest number less than or equal to a given integer 'n' that has monotone increasing digits. A number has monotone increasing digits if, for any two adjacent digits x and y, x <= y. For example, 1234 is a monotone increasing number, while 332 is not. We need to return the largest monotone increasing number that is no greater than the input 'n'.

Solution Approach

The provided solution converts the integer 'n' into a string (or character array). It then iterates through the string from right to left, looking for violations of the monotone increasing property. If a violation is found (i.e., s[i-1] > s[i]), the digit s[i-1] is decremented, and a 'marker' is set to the current index 'i'. This marker indicates the starting point for converting the remaining digits to the right into '9's. After the initial scan, the algorithm iterates from the 'marker' to the end of the string, setting all digits to '9'. Finally, the string is converted back to an integer and returned.

Step-by-Step Algorithm

  1. Step 1: Convert the input integer 'n' to a string (or character array) 's'.
  2. Step 2: Initialize a 'marker' variable to the length of the string. This variable will store the index from which we need to change digits to '9'.
  3. Step 3: Iterate through the string 's' from right to left (from index length - 1 to 1).
  4. Step 4: In each iteration, compare s[i-1] and s[i]. If s[i-1] > s[i], it means we have a violation of the monotone increasing property.
  5. Step 5: If a violation is found, decrement s[i-1] by 1 (converting it back to a character after decrementing the integer representation). Update the 'marker' to be the current index 'i'.
  6. Step 6: After the first loop finishes, iterate through the string from the 'marker' to the end of the string.
  7. Step 7: In this second loop, set each digit s[i] to '9'.
  8. Step 8: Convert the modified string 's' back to an integer and return the result.

Key Insights

  • Insight 1: The core idea is to scan the number from right to left, and if we find a digit that is smaller than the digit to its left, we need to decrement the left digit. This might create a cascade effect, requiring us to iterate backwards until we find the point where the monotone increasing property is violated.
  • Insight 2: Once a violation is found and the necessary decrements are made, all digits to the right of the decremented digit must be set to '9' to maximize the number while still satisfying the monotone increasing constraint.
  • Insight 3: Converting the number to a string or character array facilitates digit-by-digit manipulation.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Math, Greedy.

Companies

Asked at: SAP.