You are given an integer num. Rearrange the digits of num such that its value is minimized and it does not contain any leading zeros.
Return the rearranged number with minimal value.
Note that the sign of the number does not change after rearranging the digits.
Example 1:
Input: num = 310 Output: 103 Explanation: The possible arrangements for the digits of 310 are 013, 031, 103, 130, 301, 310. The arrangement with the smallest value that does not contain any leading zeros is 103.
Example 2:
Input: num = -7605 Output: -7650 Explanation: Some possible arrangements for the digits of -7605 are -7650, -6705, -5076, -0567. The arrangement with the smallest value that does not contain any leading zeros is -7650.
Constraints:
-1015 <= num <= 1015When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to find the smallest rearranged number is to try every single possible arrangement of its digits. We generate all possible numbers and then pick the smallest one that doesn't have leading zeros (unless the original number was zero).
Here's how the algorithm would work step-by-step:
from itertools import permutations
def find_smallest_rearranged_number_brute_force(number):
number_string = str(number)
digits = list(number_string)
all_permutations = permutations(digits)
smallest_number = float('inf')
for permutation in all_permutations:
arranged_number_string = "".join(permutation)
# Check for leading zeros, this step ensures a valid number
if len(arranged_number_string) > 1 and arranged_number_string[0] == '0':
continue
arranged_number = int(arranged_number_string)
# Need to handle the edge case of the original number being zero
if number == 0:
smallest_number = 0
break
#Find the absolute smallest valid permutation
smallest_number = min(smallest_number, arranged_number)
if smallest_number == float('inf'):
return 0
else:
return smallest_numberThe key is to arrange the digits to form the smallest possible number while avoiding leading zeros. We'll handle positive and negative numbers differently, focusing on placing the smallest non-zero digit first for positive numbers and the largest digit (closest to zero) after the negative sign for negative numbers.
Here's how the algorithm would work step-by-step:
def find_smallest_rearranged_number(number):
is_negative = number < 0
number = abs(number)
digit_counts = [0] * 10
for digit_char in str(number):
digit = int(digit_char)
digit_counts[digit] += 1
if is_negative:
result = "-"
# Negative number: find largest digit to put after '-'
for digit in range(9, -1, -1):
if digit_counts[digit] > 0:
result += str(digit)
digit_counts[digit] -= 1
break
# Append remaining digits in decreasing order
for digit in range(9, -1, -1):
result += str(digit) * digit_counts[digit]
return int(result)
else:
# Positive number: find smallest non-zero digit
for digit in range(1, 10):
if digit_counts[digit] > 0:
result = str(digit)
digit_counts[digit] -= 1
break
#Append the zeros to minimize value.
result += "0" * digit_counts[0]
# Append remaining digits in increasing order
for digit in range(1, 10):
result += str(digit) * digit_counts[digit]
return int(result)| Case | How to Handle |
|---|---|
| Input is zero | Return 0 directly, as there's nothing to rearrange. |
| Input is a single digit number | Return the number itself since no rearrangement is needed. |
| Input contains leading zeros after rearrangement | Ensure the first non-zero digit is placed at the beginning of the number. |
| Input is a negative number | Handle the negative sign separately and find the largest value of the remaining digits (instead of smallest). |
| Input is close to the maximum or minimum integer value | Be wary of integer overflow when rearranging and converting back to an integer. |
| Input has many zeros | Ensure that zeros are placed after the first non-zero digit. |
| Input contains both positive and negative digits (treated as string) | This is an invalid input - the prompt specifies an integer input, so either throw an exception or return an error value. |
| All digits are the same | Arrange the digits in ascending order which will result in the number itself. |