diff options
author | Jack Sangdahl <[email protected]> | 2025-03-01 00:10:01 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-01 00:10:01 -0700 |
commit | cb847deddeb96a01211dd1e4c4ea9c7c16a29109 (patch) | |
tree | 19fcc19b8d2b39ab1c9447893771f0c279f5dec1 | |
parent | 1bb2da19c46df3878ed2993e0da4c961f94622e1 (diff) | |
download | leetcode-cb847deddeb96a01211dd1e4c4ea9c7c16a29109.tar.gz leetcode-cb847deddeb96a01211dd1e4c4ea9c7c16a29109.zip |
2460. apply operations to an array
-rw-r--r-- | apply_operations_array.c | 42 | ||||
-rw-r--r-- | apply_operations_array.py | 35 |
2 files changed, 77 insertions, 0 deletions
diff --git a/apply_operations_array.c b/apply_operations_array.c new file mode 100644 index 0000000..4cd8159 --- /dev/null +++ b/apply_operations_array.c @@ -0,0 +1,42 @@ +// 2460. Apply Operations to an Array +#include "leetcode.h" + +/* + * you are given 0-indexed array 'nums' of size 'n' consisting of non-negative + * integers. you need to apply 'n - 1' operations to this array where in the + * i'th operation (0-indexed), you will apply the following on the i'th element + * of 'nums': if 'nums[i] == nums[i + 1]', then multiply 'nums[i]' by 2 and set + * 'nums[i + 1]' to operate 0. otherwise, you skip this operation. after + * performing all the operations, shift all the 0's to the end of the array. + * return the resulting array. note that the operations are applied sequentially + * not all at once. + */ + +int *applyOperations(int *nums, int numsSize, int *returnSize) { + for (int i = 0; i < numsSize - 1; i++) + if (nums[i] == nums[i + 1] && nums[i]) { + nums[i] *= 2; + nums[i + 1] = 0; + } + int *ans = (int *)malloc(numsSize * sizeof(int)), j = 0; + *returnSize = numsSize; + for (int i = 0; i < numsSize; i++) + if (nums[i]) + ans[j++] = nums[i]; + while (j < numsSize) + ans[j++] = 0; + return ans; +} + +int main() { + int n1[] = {1, 2, 2, 1, 1, 0}, n2[] = {0, 1}, rs1, rs2; + int *ao1 = applyOperations(n1, ARRAY_SIZE(n1), &rs1); + int *ao2 = applyOperations(n2, ARRAY_SIZE(n2), &rs2); + for (int i = 0; i < rs1; i++) + printf("%d ", ao1[i]); // expect: 1 4 2 0 0 0 + printf("\n"); + for (int i = 0; i < rs2; i++) + printf("%d ", ao2[i]); // expect: 1 0 + printf("\n"); + free(ao1), free(ao2); +} diff --git a/apply_operations_array.py b/apply_operations_array.py new file mode 100644 index 0000000..3dc8e7c --- /dev/null +++ b/apply_operations_array.py @@ -0,0 +1,35 @@ +# 2460. Apply Operations to an Array + +""" +you are given 0-indexed array 'nums' of size 'n' consisting of non-negative +integers. you need to apply 'n - 1' operations to this array where in the +i'th operation (0-indexed), you will apply the following on the i'th element +of 'nums': if 'nums[i] == nums[i + 1]', then multiply 'nums[i]' by 2 and set +'nums[i + 1]' to operate 0. otherwise, you skip this operation. after +performing all the operations, shift all the 0's to the end of the array. +return the resulting array. note that the operations are applied sequentially +not all at once. +""" + + +class Solution(object): + def applyOperations(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + j = 0 + for i in range(len(nums)): + if i + 1 < len(nums) and nums[i] == nums[i + 1]: + nums[i] *= 2 + nums[i + 1] = 0 + if nums[i]: + nums[j], nums[i] = nums[i], nums[j] + j += 1 + return nums + + +if __name__ == "__main__": + obj = Solution() + print(obj.applyOperations(nums=[1, 2, 2, 1, 1, 0])) + print(obj.applyOperations(nums=[0, 1])) |