diff options
author | Jack Sangdahl <[email protected]> | 2025-04-07 00:58:49 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-04-07 00:58:49 -0600 |
commit | 3893ea81d705aff59c158cfdafb33a04ee748627 (patch) | |
tree | 1030e9f6b8a506b85fcf36c2177b4e8ba76fe84e /max_diff_change_int.py | |
parent | beff35cecb8082a45325921e6f97179976808fe6 (diff) | |
download | leetcode-3893ea81d705aff59c158cfdafb33a04ee748627.tar.gz leetcode-3893ea81d705aff59c158cfdafb33a04ee748627.zip |
1432. max difference you can get from changing an integer
Diffstat (limited to 'max_diff_change_int.py')
-rw-r--r-- | max_diff_change_int.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/max_diff_change_int.py b/max_diff_change_int.py new file mode 100644 index 0000000..fbf9fb0 --- /dev/null +++ b/max_diff_change_int.py @@ -0,0 +1,35 @@ +# 1432. Max Difference You Can Get From Changing an Integer + +""" +you are given an integer 'num'. you will apply the following steps exactly +two times: pick a digit 'x (0 <= x <= 9)', pick another digit 'y (0 <= y <= +9)'. the digit 'y' can be equal to 'x'. replace all occurences of 'x' in the +decimal representation of 'num' by 'y'. the new integer cannot have any +leading zeros. also the new integer cannot be 0. let 'a' and 'b' be the +results of applying the operations to 'num' the first and second times +respectively. return the max difference between 'a' and 'b'. +""" + + +class Solution(object): + def maxDiff(self, num): + """ + :type num: int + :rtype: int + """ + n = str(num) + maxn, minn = float("-inf"), float("inf") + for i in "0123456789": + for j in "0123456789": + next = n.replace(i, j) + if next[0] == "0" or int(next) == 0: + continue + maxn = max(maxn, int(next)) + minn = min(minn, int(next)) + return maxn - minn + + +if __name__ == "__main__": + obj = Solution() + print(obj.maxDiff(num=555)) + print(obj.maxDiff(num=9)) |