[python] Remove Digit From Number to Maximize Result leetcode 2259

Designated Hitter Jack·2023년 10월 30일
0

leetcode

목록 보기
5/11
post-thumbnail

문제

  1. Remove Digit From Number to Maximize Result

User Accepted:7271
User Tried:8673
Total Accepted:7459
Total Submissions:18729
Difficulty:Easy

You are given a string number representing a positive integer and a character digit.

Return the resulting string after removing exactly one occurrence of digit from number such that the value of the resulting string in decimal form is maximized. The test cases are generated such that digit occurs at least once in number.

Example 1:
  Input: number = "123", digit = "3"
  Output: "12"
  Explanation: There is only one '3' in "123". After removing '3', the result is "12".

Example 2:
  Input: number = "1231", digit = "1"
  Output: "231"
  Explanation: We can remove the first '1' to get "231" or remove the second '1' to get "123".
  Since 231 > 123, we return "231".

Example 3:
  Input: number = "551", digit = "5"
  Output: "51"
  Explanation: We can remove either the first or second '5' from "551".
  Both result in the string "51".
 

Constraints:
  2 <= number.length <= 100
  number consists of digits from '1' to '9'.
  digit is a digit from '1' to '9'.
  digit occurs at least once in number.

풀이

class Solution:
    def removeDigit(self, number: str, digit: str) -> str:
        result = []
        number_list = list(number)
        count = number_list.count(digit)
        
        for idx, number in enumerate(number_list):
            if number == digit:
                number_list[idx] = ''
                result.append("".join(number_list))
                number_list[idx] = number
            
        return max(result)

digit에 해당하는 숫자를 뺀 경우를 리스트에 전부 넣고 이에 대하여 최대값을 비교하는 방식으로 풀었다.
digit에 해당하는 숫자의 다음 자리가 digit보다 더 작은 경우는 고려하지 않는 등 좀 더 스마트한 방식이 있을 것 같지만 일단 이렇게도 풀리니까..

profile
Fear always springs from ignorance.

0개의 댓글