diff options
author | jack <[email protected]> | 2024-06-20 19:54:24 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-20 19:54:24 -0600 |
commit | bd945a7d02858be19b5fa2a80cc4782dda112d50 (patch) | |
tree | 48ca87275d59e90ad7e2922fefea99587e75834c | |
parent | ab7c69280c3dca736e00e6b41372167045af27cc (diff) | |
download | leetcode-bd945a7d02858be19b5fa2a80cc4782dda112d50.tar.gz leetcode-bd945a7d02858be19b5fa2a80cc4782dda112d50.zip |
2654. mininimum operations array elements 1
-rw-r--r-- | min_num_opp_arr_equal_1.c | 48 | ||||
-rw-r--r-- | min_num_opp_arr_equal_1.py | 36 |
2 files changed, 84 insertions, 0 deletions
diff --git a/min_num_opp_arr_equal_1.c b/min_num_opp_arr_equal_1.c new file mode 100644 index 0000000..c6b5dc3 --- /dev/null +++ b/min_num_opp_arr_equal_1.c @@ -0,0 +1,48 @@ +// 2654. Minimum Number of Operations to Make All Array Elements Equal to 1 +#include "leetcode.h" + +/* + * given a 0-indexed array 'nums' consisting of positive integers. you can do + * the following operation on the array any number of time: select an index 'i' + * such that '0 <= i < n - 1' and replace either of 'nums[i]' or 'nums[i + 1]' + * with their gcd values. return the minimum number of operations to make all + * elements of 'nums' equal to 1. if this is impossible, return -1. the gcd of + * two integers is the greatest common divisor of the two integers. + */ + +int gcd(int a, int b) { + int rem = a % b; + if (!rem) + return b; + return gcd(b, rem); +} + +int minOperations(int *nums, int numsSize) { + int one_cnt = 0; + for (int i = 0; i < numsSize; i++) + if (nums[i] == 1) + one_cnt++; + if (one_cnt >= 1) + return numsSize - one_cnt; + int min_subarr_len = numsSize + 1; + for (int i = 0; i < numsSize; i++) { + int curr = nums[i]; + for (int j = i + 1; j < numsSize; j++) { + curr = gcd(curr, nums[j]); + if (curr == 1) { + int curr_n = (j + 1) - i; + if (curr_n < min_subarr_len) + min_subarr_len = curr_n; + } + break; + } + } + return min_subarr_len <= numsSize ? (min_subarr_len - 1) + (numsSize - 1) + : -1; +} + +int main() { + int n1[] = {2, 6, 3, 4}, n2[] = {2, 10, 6, 14}; + printf("%d\n", minOperations(n1, ARRAY_SIZE(n1))); // expect: 4 + printf("%d\n", minOperations(n2, ARRAY_SIZE(n2))); // expect: -1 +} diff --git a/min_num_opp_arr_equal_1.py b/min_num_opp_arr_equal_1.py new file mode 100644 index 0000000..75551dc --- /dev/null +++ b/min_num_opp_arr_equal_1.py @@ -0,0 +1,36 @@ +# 2654. Minimum Number of Operations to Make All Array Elements Equal to 1 +from math import gcd + +""" +given a 0-indexed array 'nums' consisting of positive integers. you can do +the following operation on the array any number of time: select an index 'i' +such that '0 <= i < n - 1' and replace either of 'nums[i]' or 'nums[i + 1]' +with their gcd values. return the minimum number of operations to make all +elements of 'nums' equal to 1. if this is impossible, return -1. the gcd of +two integers is the greatest common divisor of the two integers. +""" + + +class Solution(object): + def minOperations(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + ones_cnt = nums.count(1) + if ones_cnt: + return len(nums) - ones_cnt + diff = float("inf") + for i in range(len(nums)): + g = nums[i] + for j in range(i + 1, len(nums)): + g = gcd(g, nums[j]) + if g == 1: + diff = min(diff, j - i) + return -1 if diff == float("inf") else diff + len(nums) - 1 + + +if __name__ == "__main__": + obj = Solution() + print(obj.minOperations([2, 6, 3, 4])) + print(obj.minOperations([2, 10, 6, 14])) |