diff options
-rw-r--r-- | max_diff_remap_digit.c | 30 | ||||
-rw-r--r-- | max_diff_remap_digit.py | 26 | ||||
-rw-r--r-- | min_max_diff_pair.c | 18 | ||||
-rw-r--r-- | min_max_diff_pair.py | 42 |
4 files changed, 106 insertions, 10 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)) diff --git a/min_max_diff_pair.c b/min_max_diff_pair.c index 6172bc7..7577e3b 100644 --- a/min_max_diff_pair.c +++ b/min_max_diff_pair.c @@ -1,6 +1,5 @@ // 2616. Minimize the Maximum Difference of Pairs -#include <stdio.h> -#include <stdlib.h> +#include "leetcode.h" /* * given a 0-indexed integer array 'nums' and an integer 'p'. find 'p' pairs of @@ -14,13 +13,12 @@ int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; } -int minimizeMax(int *nums, int nums_size, int p) { - qsort(nums, nums_size, sizeof(int), cmp); - int i, j, left = 0, right = nums[nums_size - 1] - nums[0]; +int minimizeMax(int *nums, int numsSize, int p) { + qsort(nums, numsSize, sizeof(int), cmp); + int i, j, left = 0, right = nums[numsSize - 1] - nums[0]; while (left < right) { - int mid = (left + right) / 2; - int cnt = 0; - for (int i = 0; i + 1 < nums_size; ++i) + int mid = (left + right) / 2, cnt = 0; + for (int i = 0; i + 1 < numsSize; ++i) if (mid >= nums[i + 1] - nums[i]) ++cnt, ++i; if (cnt >= p) @@ -33,6 +31,6 @@ int minimizeMax(int *nums, int nums_size, int p) { int main() { int n1[] = {10, 1, 2, 7, 1, 3}, n2[] = {4, 2, 1, 2}; - printf("%d\n", minimizeMax(n1, 6, 2)); // expect: 1 - printf("%d\n", minimizeMax(n2, 4, 1)); // expect: 0 + printf("%d\n", minimizeMax(n1, ARRAY_SIZE(n1), 2)); // expect: 1 + printf("%d\n", minimizeMax(n2, ARRAY_SIZE(n2), 1)); // expect: 0 } diff --git a/min_max_diff_pair.py b/min_max_diff_pair.py new file mode 100644 index 0000000..8c5478d --- /dev/null +++ b/min_max_diff_pair.py @@ -0,0 +1,42 @@ +# 2616. Minimize the Maximum Difference of Pairs + +""" +given a 0-indexed integer array 'nums' and an integer 'p'. find 'p' pairs of +indices of 'nums' such that the maximum difference amongst all the pairs is +minimised. also, ensure no index appears more than once amongst that 'p' +pairs. note that for a pair of elements at the index 'i' and 'j'. the +difference of this pair is '|nums[i] - nums[j]|', where '|x|' represents the +absolute value of 'x'. return the minimum maximum difference among all 'p' +pairs. we define the maximum of an empty set to be zero. +""" + + +class Solution(object): + def minimizeMax(self, nums, p): + """ + :type nums: List[int] + :type p: int + :rtype: int + """ + nums.sort() + n = len(nums) + left, right = 0, nums[-1] - nums[0] + while left < right: + mid = (left + right) // 2 + i, j = 1, 0 + while i < n: + if nums[i] - nums[i - 1] <= mid: + i += 1 + j += 1 + i += 1 + if j >= p: + right = mid + else: + left = mid + 1 + return left + + +if __name__ == "__main__": + obj = Solution() + print(obj.minimizeMax(nums=[10, 1, 2, 7, 1, 3], p=2)) + print(obj.minimizeMax(nums=[4, 2, 1, 2], p=1)) |