Hello, reader ๐๐ฝ ! Welcome to day 37 of the series on Problem Solving. Through this series, I aim to pick up at least one question everyday and share my approach for solving it.
Today, I will be picking up LeetCode's daily challenge problem: 1323. Maximum 69 Number.
๐ค Problem Statement
- Given a positive number containing only digits 6 and 9, return the maximum number that can be obtained after modifying at most one digit.
A number can be modified from 6 to 9 or 9 to 6.
E.g.:
num = 9669
=>9969
num = 9999
=>9999
num = 669
=>969
๐ฌ Thought Process - Convert to String
- From intuition, we can get the maximum number by converting the first 6 to 9.
- For e.g., we can obtain the maximum number from this
9669
by modifying the 6 at 100's place to 9. - The easiest way to change this would be to convert it to string and then to a character array.
- We will scan from left to right, update the first 6 we see to 9, and then convert the array back to string and then to integer and return.
๐ฉ๐ฝโ๐ป Solution - Convert to String
class Solution {
public int maximum69Number (int num) {
char[] nums = Integer.toString(num).toCharArray();
for(int i = 0; i<nums.length; i++) {
if(nums[i] == '6') {
nums[i] = '9';
break;
}
}
return Integer.valueOf(new String(nums));
}
}
Time Complexity: O(N)
N = number of digits in num
Space Complexity: O(N)
N = number of digits in num
- We will have to use additional space to convert the number to an Interger and character array.
๐ฌ Thought Process - Get Index of First 6
- This is the optimisation you might have to come up if you get this in an interview.
- Instead of using extra space, we will keep getting the number at 1s, 10s, 100s, 1000s, place and so on.
- Whenever we encounter a 6, we will note down the digit number. Then we will add
3 * 10^digit
to the original number. - For e.g.:
num = 996
.- the first digit index for 6 would be at 0.
- we add
3 * 10^0 = 3
to996
. Hence996 + 3 = 999
. 999
is the greatest number that can be formed by modifying at most 1 number in996
.
For e.g.:
num = 9669
.- the first 6 is found at index 2.
- we add
3 * 10^2 = 300
to9669
. Hence9669 + 300 = 9969
. 9969
is the greatest number that can be formed by modifying at most 1 number in9669
.
One edge case is if we have no 6s in the number.
- To address this, we will initialise the index with -1.
- Then once we have looped through the number, if the index is still -1, we return the original number itself.
๐ฉ๐ฝโ๐ป Solution - Get Index of First 6
class Solution {
public int maximum69Number (int num) {
int n = num;
int index = -1;
int currentDigit = 0;
while(n > 0) {
int dig = n % 10;
if(dig == 6) {
index = currentDigit;
}
currentDigit++;
n /= 10;
}
if(index == -1) return num;
int tens = (int)(Math.pow(10, index));
return num + 3 * tens;
}
}
Time Complexity: O(N)
N = number of digits in num
Space Complexity: O(1)
- You can find the link to the GitHub repo for this question here: 1323. Maximum 69 Number.
Conclusion
That's a wrap for today's problem. If you liked my explanation then please do drop a like/ comment. Also, please correct me if I've made any mistakes or if you want me to improve something!
Thank you for reading!