24 lines
778 B
Python
24 lines
778 B
Python
# https://leetcode.com/problems/maximum-difference-by-remapping-a-digit
|
|
|
|
from typing import List, Optional
|
|
|
|
class Solution:
|
|
def minMaxDifference(self, num: int) -> int:
|
|
n: List[str] = list(str(num))
|
|
max_num: List[str] = []
|
|
min_num: List[str] = []
|
|
max_target: Optional[str] = None
|
|
min_target: str = n[0]
|
|
for i in range(len(n)):
|
|
if n[i] == min_target:
|
|
min_num.append('0')
|
|
else:
|
|
min_num.append(n[i])
|
|
if max_target is None and n[i] != '9':
|
|
max_target = n[i]
|
|
if max_target == n[i]:
|
|
max_num.append('9')
|
|
else:
|
|
max_num.append(n[i])
|
|
return int(''.join(max_num)) - int(''.join(min_num))
|
|
|