지난 공부기록/알고리즘 풀이

Python - Algorithm #31

hkl22 2024. 8. 2. 10:11

Python Algorithm

LeetCode

2089. Find Target Indices After Sorting Array

  • You are given a 0-indexed integer array [nums] and a target element target.
  • A target index is an index i such that nums[i] == target.
  • Return a list of the target indices of [nums] after sorting [nums] in non-decreasing order. If there are no target indices, return an empty list. The returned list must be sorted in increasing order.
  • Constraints
    • 1 nums.length 100
    • 1 nums[i], target 100
class Solution:
    def targetIndices(self, nums: List[int], target: int) -> List[int]:
        # for문
        answer = []
        nums = sorted(nums)
        for i, num in enumerate(nums):
            # num이 target과 같을 경우, answer에 index를 추가해 볼 것
            if num == target:
                answer.append(i)
        return answer
class Solution:
    def targetIndices(self, nums: List[int], target: int) -> List[int]:
        return [i for i, x in enumerate(sorted(nums)) if x == target]
class Solution:
    def targetIndices(self, nums: List[int], target: int) -> List[int]:
        answer = []
        nums.sort()
        for i in range(len(nums)):
            if nums[i] == target:
                answer.append(i)
        return answer

2566. Maximum Difference by Remapping a Digit

  • You are given an integer num. You know that Bob will sneakily remap one of the 10 possible digits (0 to 9) to another digit.
  • Return the difference between the maximum and minimum values Bob can make by remapping exactly one digit in num.
  • Notes:
    • When Bob remaps a digit d1 to another digit d2, Bob replaces all occurrences of d1 in num with d2.
    • Bob can remap a digit to itself, in which case num does not change.
    • Bob can remap different digits for obtaining minimum and maximum values respectively.
    • The resulting number after remapping can contain leading zeroes.
  • Constraints
    • 1 num 10⁸
class Solution:
    def minMaxDifference(self, num: int) -> int:
        # 가장 큰 수는 무조건 9로 치환한 수
        # 가장 작은 수는 무조건 0으로 치환한 수
        nums_str = str(num)
        candidates = []
        for i in range(10):
            candidates.append(int(nums_str.replace(str(i), "9")))
            candidates.append(int(nums_str.replace(str(i), "0")))
        return max(candidates) - min(candidates)
class Solution:
    def minMaxDifference(self, num: int) -> int:
        num_str = str(num)
        max_num = num_str
        min_num = num_str
        for char in num_str:
            if char != "9":
                max_num = num_str.replace(char, "9")
                break
        for char in num_str:
            if char != "0":
                min_num = num_str.replace(char, "0")
                break
        return int(max_num) - int(min_num)
class Solution:
    def minMaxDifference(self, num: int) -> int:
        nums = str(num)
        m = int(nums.replace(nums[0], "0"))
        for n in nums:
            if n != "9":
                return int(nums.replace(n, "9")) - m
        return num - m

https://leetcode.com/problems/find-target-indices-after-sorting-array/

https://leetcode.com/problems/maximum-difference-by-remapping-a-digit/

'지난 공부기록 > 알고리즘 풀이' 카테고리의 다른 글

Python - Algorithm #33  (0) 2024.08.06
Python - Algorithm #32  (0) 2024.08.05
Python - Algorithm #30  (0) 2024.08.01
Python - Algorithm #29  (0) 2024.07.31
Python - Algorithm #28  (0) 2024.07.30