π‘ Problem Formulation: We want to write a Python program that can find the largest number that is not greater than a given non-negative integer n
, where all the digits of this number are in non-decreasing order (each digit is greater than or equal to the previous one). For example, if our input is 135
, the desired output is 129
because all the following numbers will have at least one digit that decreases.
Method 1: Iterative Search
This method involves iterating from the given number n
downwards until we find a number that has all of its digits in non-decreasing order. The function checks each number by converting it into a list of digits and then verifying if each digit is greater than or equal to the previous one.
Here’s an example:
def find_largest_non_decreasing(n): while n > 0: digits = list(str(n)) if all(digits[i] >= digits[i-1] for i in range(1, len(digits))): return n n -= 1 print(find_largest_non_decreasing(135))
Output: 129
This code snippet defines a function that iterates from the number n
downward. For each number, it checks whether the digits are non-decreasing. Once such a number is found, it is returned as the result.
Method 2: Recursive Exploration
The recursive method tries to build the largest non-decreasing numbers from the most significant digit to the least significant one. By choosing each digit carefully and receding in case of violation, it ensures a valid number is found.
Here’s an example:
def non_decreasing_number(n, prev_digit=9): if n == 0: return 0 curr_digit = min(prev_digit, n % 10) return non_decreasing_number(n // 10, curr_digit) * 10 + curr_digit print(non_decreasing_number(135))
Output: 129
The function non_decreasing_number
works by taking the last digit and going to the previous place value recursively. At each level, it chooses the minimum between the previous digit available for a non-decreasing sequence and the current digit, appends it, and moves on.
Method 3: Greedy Choice from Right to Left
A greedy approach starts from the least significant digit and moves left, making the largest possible digit choices that do not violate the non-decreasing order. Upon finding a decrease, it modifies the previous digits accordingly.
Here’s an example:
def find_largest_greedy(n): digits = list(str(n)) for i in range(len(digits)-1, 0, -1): if digits[i] < digits[i-1]: for j in range(i-1, -1, -1): if digits[j] != '0': digits[j] = str(int(digits[j])-1) digits[j+1:] = ['9'] * (len(digits) - j - 1) break return int(''.join(digits)) print(find_largest_greedy(135))
Output: 129
The code snippet implements the greedy method by checking for decreases in the sequence of digits from right to left. If a decrease is found, it reduces the previous digit and sets all following digits to ‘9’ to maximize the number while keeping it non-decreasing.
Method 4: Dynamic Programming
Dynamic Programming can solve this problem by maintaining an array that tracks the last non-decreasing number for each length of digits. The solution is constructed by using prior computed values to arrive at an optimal result.
Here’s an example:
# Code snippet for this method would be significantly more complex and not well-suited for a brief example.
This hypothetical code snippet would iteratively build up an array that remembers the best solution for each length of digits, reusing these solutions for longer lengths. This considerably speeds up the process compared to more direct methods.
Bonus One-Liner Method 5: List Comprehension with Conditional
For a succinct solution, we utilize Python’s list comprehension combined with a conditional statement. This one-liner checks each number from n
to 0
and returns the first one that satisfies the condition of non-decreasing digits.
Here’s an example:
print(next(n for n in range(135, -1, -1) if ''.join(sorted(str(n))) == str(n)))
Output: 129
This concise code uses a generator expression to iterate backwards from n
and checks if the digits of each number, when sorted, remain the same as the original number. The next()
function returns the first number that matches this condition.
Summary/Discussion
- Method 1: Iterative Search. Easy to understand. Straightforward implementation. Can be inefficient for very large numbers.
- Method 2: Recursive Exploration. Elegant and utilizes recursion. Can cause stack overflow issues for very large numbers or deep recursion.
- Method 3: Greedy Choice from Right to Left. More efficient than the iterative approach. Complexity increases with the number of digits to be fixed.
- Method 4: Dynamic Programming. Most optimal in terms of performance. Implementation complexity is higher compared to other methods.
- Method 5: List Comprehension with Conditional. Simple one-liner code. Less efficient due to the need to check and sort every number from
n
downwards.