[LeetCode] Daily LeetCode: 1323

devbang·2023년 9월 20일
0

LeetCode

목록 보기
3/16

1323. Maximum 69 Number

Problem type - Greedy

🧩 Problem

🎯 Strategy

Setting up the logic

We need to understand the concept and the details of this problem. According to examples, we can derive that checking each digit and changing the biggest digit, which is 6, is the answer.

Replace method

Python has a useful function on this problem: .replace().

In order to use the replace function, we need to change the number into a string. str(num)

Then, we can assign it to the variable result and use the replace function.

To replace the function's variables, we put the string to search for(old value), the string to replace the old value, and how many old values you want to replace.

From our logic, we are finding the biggest digit, that is '6'.

def maximum69Number (self, num: int) -> int:
	result = str(num).replace("6", "9", 1)

To conclude, we can simply return with the replaced integer value.

def maximum69Number (self, num: int) -> int:
 	result = str(num).replace("6", "9", 1)
    return int(result)

List method

The previous method was simple, but we couldn't practice the basic logic since we used the Python library.

So, let's use the list method to prepare a general coding interview.

def maximum69Number (self, num: int) -> int:
	numList = list(str(num))
    
    for i in range(len(numList)):

To iterate over the num, we have to put the single numbers in the list as a string. Then, iterate over each number in the range of the length of the list.

Each index is a digit, and we want to know the biggest digit, which is 6. We will stop the loop once we find the biggest digit, 6.

def maximum69Number (self, num: int) -> int:
	numList = list(str(num))
    
    for i in range(len(numList)):
    	if numList[i] == '6':
        	numList[i] = '9'
            break
            
	return int("".join(numList))

Lastly, we can return the modified list by using the .join() method to obtain the final numbers.

📌 Thoughts

I spent time considering how to iterate through the number, but I realized that iterating over the 'num' itself was not possible. Therefore, I attempted to use the second method, which could be applied in various programming languages.

💻 Solution

List method

 def maximum69Number (self, num: int) -> int:
        numList = list(str(num))
        
        for i in range(len(numList)):
        	if numList[i] == '6':
            	numList[i] = '9'
                break
		
        return int("".join(numList))

Python replace Method

 def balancedStringSplit(self, s: str) -> int:
 	result = str(num).replace('6', '9', 1)
    
    return int(result)
profile
뛰어난 개발자보다는 꾸준히 발전하는 개발자가 되자

0개의 댓글