Similar Problems
Similar Problems not available
Remove Digit From Number To Maximize Result - Leetcode Solution
Companies:
LeetCode: Remove Digit From Number To Maximize Result Leetcode Solution
Difficulty: Easy
Problem statement:
Given an integer n, you can remove a single digit from it to get a new integer. Your task is to find the maximum possible value of this new integer.
Example:
Input: n = 2736 Output: 736 Explanation: You can remove the digit '2' to get the new integer 736, which is the maximum possible value.
Solution approach:
We can solve this problem by iterating through each digit of the given integer n and removing it, computing the value of the resulting integer, and storing the maximum value we find. In order to remove a digit from the integer, we can convert it to a string, remove the character at the given index, and then convert it back to an integer.
- Convert the integer n to a string and store it in a variable.
- Initialize a variable max_result to 0.
- For each digit in the string representation of n, do the following: a. Remove the digit from the string n. b. Convert the resulting string back to an integer and store it in a variable new_result. c. Update the value of max_result to be the maximum value between max_result and new_result.
- Return the value of max_result.
Time Complexity:
The time complexity of this solution is O(n^2), where n is the number of digits in the given integer. This is because we are iterating through each digit in the string representation of the integer and removing it, which takes O(n) time. Additionally, converting the string back to an integer also takes O(n) time.
Code:
Here is the Python implementation of the above solution:
def removeDigit(n): n_str = str(n) max_result = 0 for i in range(len(n_str)): new_n_str = n_str[:i] + n_str[i+1:] new_result = int(new_n_str) max_result = max(max_result, new_result) return max_result
Sample Test
print(removeDigit(2736)) # 736
Remove Digit From Number To Maximize Result Solution Code
1