aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-06-13 23:29:00 -0600
committerJack Sangdahl <[email protected]>2025-06-13 23:29:18 -0600
commitd39bae7e2e70b5658e96204f9bd61f4e7cb25d48 (patch)
tree831d34b7a3046025d8cd29938a902f47156610a3
parent24a12cfb2ca977ff8792489a54d9136b49e4f740 (diff)
downloadleetcode-d39bae7e2e70b5658e96204f9bd61f4e7cb25d48.tar.gz
leetcode-d39bae7e2e70b5658e96204f9bd61f4e7cb25d48.zip
2566. maximum difference by remapping a digit
-rw-r--r--max_diff_remap_digit.c30
-rw-r--r--max_diff_remap_digit.py26
2 files changed, 56 insertions, 0 deletions
diff --git a/max_diff_remap_digit.c b/max_diff_remap_digit.c
new file mode 100644
index 0000000..3eff54a
--- /dev/null
+++ b/max_diff_remap_digit.c
@@ -0,0 +1,30 @@
+// 2566. Maximum Difference by Remapping a Digit
+#include "leetcode.h"
+
+/*
+ * you are given an integer 'num'. you know that bob will sneakily remap one of
+ * the 10 possible digits to another digit. return the difference between the
+ * maximum and minimum values bob can make by remapping exactly one digit in
+ * nums.
+ */
+
+int minMaxDifference(int num) {
+ int min = num, max = num;
+ for (int i = 0; i < 10; ++i) {
+ int low = 0, high = 0, mult = 1;
+ for (int j = num; j; j /= 10) {
+ bool replace = j % 10 == i;
+ low += (replace ? 0 : j % 10) * mult;
+ high += (replace ? 9 : j % 10) * mult;
+ mult *= 10;
+ }
+ min = fmin(min, low);
+ max = fmax(max, high);
+ }
+ return max - min;
+}
+
+int main() {
+ printf("%d\n", minMaxDifference(11891)); // expect: 99009
+ printf("%d\n", minMaxDifference(90)); // expect: 99
+}
diff --git a/max_diff_remap_digit.py b/max_diff_remap_digit.py
new file mode 100644
index 0000000..5d802ed
--- /dev/null
+++ b/max_diff_remap_digit.py
@@ -0,0 +1,26 @@
+# 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 to another digit. return the difference between the
+maximum and minimum values bob can make by remapping exactly one digit in
+nums.
+"""
+
+
+class Solution(object):
+ def minMaxDifference(self, num):
+ """
+ :type num: int
+ :rtype: int
+ """
+ num, i = str(num), 0
+ while num[i] == "9" and i < len(num) - 1:
+ i += 1
+ return int(num.replace(num[i], "9")) - int(num.replace(num[0], "0"))
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.minMaxDifference(num=11891))
+ print(obj.minMaxDifference(num=90))