-
Notifications
You must be signed in to change notification settings - Fork 0
/
high-five.py
30 lines (24 loc) · 914 Bytes
/
high-five.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# https://binarysearch.com/problems/High-Five
# to make a number as small as possible, put 5 to the left of the first digit from the left that's > 5
# to make a number as large as possible, put a 5 at the left of the first digit from the left that's < 5
class Solution:
def split(self, n):
return [c for c in str(n)]
def make_smallest(self, n):
s = self.split(n)
for i in range(len(s)):
if s[i] > '5':
return s[:i] + ['5'] + s[i:]
return s + ['5']
def make_largest(self, n):
s = self.split(n)
for i in range(len(s)):
if s[i] < '5':
return s[:i] + ['5'] + s[i:]
return s + ['5']
def solve(self, n):
result = self.make_largest(n) if n >= 0 else self.make_smallest(-n)
result = int(''.join(result))
if n < 0:
result = -result
return result