This commit is contained in:
parent
47ddfc37e5
commit
f981ba7f45
1 changed files with 24 additions and 0 deletions
24
easy/maximum_difference_by_remapping_a_digit.py
Normal file
24
easy/maximum_difference_by_remapping_a_digit.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# 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))
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue