diff options
author | Jack Sangdahl <[email protected]> | 2024-10-25 13:27:03 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-10-26 00:38:09 -0600 |
commit | 5ea66938849e5a07d6d7c9d20256421d43680457 (patch) | |
tree | 7789796c13919d273eae7d390fd7b90939e03cda | |
parent | a43f21d0aff66470e17b47febcd01e67bbdce5f6 (diff) | |
download | leetcode-5ea66938849e5a07d6d7c9d20256421d43680457.tar.gz leetcode-5ea66938849e5a07d6d7c9d20256421d43680457.zip |
2967. minimum cost to make array equalidromic
-rw-r--r-- | min_cost_arr_equalindromic.c | 64 | ||||
-rw-r--r-- | min_cost_arr_equalindromic.py | 40 |
2 files changed, 104 insertions, 0 deletions
diff --git a/min_cost_arr_equalindromic.c b/min_cost_arr_equalindromic.c new file mode 100644 index 0000000..a46f325 --- /dev/null +++ b/min_cost_arr_equalindromic.c @@ -0,0 +1,64 @@ +// 2967. Minimum Cost to Make Array Equalindromic +#include "leetcode.h" + +/* + * you are given a 0-indexed integer array 'nums' having length 'n'. you are + * allowed to perform a special move any number of times on 'nums'. in one + * special move, you perform the following steps in order: choose an index 'i' + * in the range '[0, n - 1]' and a positive integer 'x', add '|nums[i] - x|' to + * the total cost, and change the value of 'nums[i]' to 'x'. a palindromic + * number is a positive integer that remains the same when its digits are + * reversed. an array is considered equalindromic if all the elements in the + * array are equal to an integer 'y', where 'y' is a palindromic number less + * than 10^9. return an integer denoting the minimum possible total cost to make + * 'nums' equalindromic by performing any number of special moves + */ + +bool is_palindrome(int num) { + if (num < 0) + return false; + int reverse = 0, original = num; + while (num) { + int digit = num % 10; + if (reverse > (INT_MAX - digit) / 10) + return false; + reverse = reverse * 10 + digit; + num /= 10; + } + return reverse == original; +} + +long long calculate_cost(int *nums, int numsSize, int target) { + long long cost = 0; + for (int i = 0; i < numsSize; i++) + cost += abs(nums[i] - target); + return cost; +} + +int cmp(const void *a, const void *b) { return (*(int *)a - *(int *)b); } + +long long minimumCost(int *nums, int numsSize) { + qsort(nums, numsSize, sizeof(int), cmp); + int median = nums[numsSize / 2]; + int lower = median - 100000, upper = median + 100000; + if (lower < 1) + lower = 1; + if (upper > 1000000000) + upper = 1000000000; + long long min_cost = LLONG_MAX; + for (int i = 0; i <= upper; i++) + if (is_palindrome(i)) { + long long curr_cost = calculate_cost(nums, numsSize, i); + if (curr_cost < min_cost) + min_cost = curr_cost; + } + return min_cost; +} + +int main() { + int n1[] = {1, 2, 3, 4, 5}, n2[] = {10, 12, 13, 14, 15}, + n3[] = {22, 33, 22, 33, 22}; + printf("%lld\n", minimumCost(n1, ARRAY_SIZE(n1))); // expect: 6 + printf("%lld\n", minimumCost(n2, ARRAY_SIZE(n2))); // expect: 11 + printf("%lld\n", minimumCost(n3, ARRAY_SIZE(n3))); // expect: 22 +} diff --git a/min_cost_arr_equalindromic.py b/min_cost_arr_equalindromic.py new file mode 100644 index 0000000..2594149 --- /dev/null +++ b/min_cost_arr_equalindromic.py @@ -0,0 +1,40 @@ +# 2967. Minimum Cost to Make Array Equalindromic + +""" +you are given a 0-indexed integer array 'nums' having length 'n'. you are +allowed to perform a special move any number of times on 'nums'. in one +special move, you perform the following steps in order: choose an index 'i' +in the range '[0, n - 1]' and a positive integer 'x', add '|nums[i] - x|' to +the total cost, and change the value of 'nums[i]' to 'x'. a palindromic +number is a positive integer that remains the same when its digits are +reversed. an array is considered equalindromic if all the elements in the +array are equal to an integer 'y', where 'y' is a palindromic number less +than 10^9. return an integer denoting the minimum possible total cost to make +'nums' equalindromic by performing any number of special moves +""" + + +class Solution(object): + def minimumCost(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + nums.sort() + palindrome = lambda x: str(x) == str(x)[::-1] + left = right = nums[len(nums) // 2] + while not palindrome(left): + left -= 1 + while not palindrome(right): + right += 1 + return min( + sum(map(lambda x: abs(x - left), nums)), + sum(map(lambda x: abs(x - right), nums)), + ) + + +if __name__ == "__main__": + obj = Solution() + print(obj.minimumCost(nums=[1, 2, 3, 4, 5])) + print(obj.minimumCost(nums=[10, 12, 13, 14, 15])) + print(obj.minimumCost(nums=[22, 33, 22, 33, 22])) |