diff options
author | Jack Sangdahl <[email protected]> | 2025-03-01 19:28:17 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-01 19:28:17 -0700 |
commit | 4087d8f9090f84fe770bea6d621f3f24b912b40c (patch) | |
tree | 3bd97041c3f12381778722dcf2544d7a2d4e5846 /merge_two_2d_arr_sum.c | |
parent | d244407ce1f87bc1bd01cd3199cc179433582e70 (diff) | |
download | leetcode-4087d8f9090f84fe770bea6d621f3f24b912b40c.tar.gz leetcode-4087d8f9090f84fe770bea6d621f3f24b912b40c.zip |
2570. merge two 2d arrays by summing values
Diffstat (limited to 'merge_two_2d_arr_sum.c')
-rw-r--r-- | merge_two_2d_arr_sum.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/merge_two_2d_arr_sum.c b/merge_two_2d_arr_sum.c new file mode 100644 index 0000000..5263902 --- /dev/null +++ b/merge_two_2d_arr_sum.c @@ -0,0 +1,89 @@ +// 2570. Merge Two 2D Arrays by Summing Values +#include "leetcode.h" + +/* + * you are given two 2d integer arrays 'nums1' and 'nums2'. 'nums1[i] = [id_i, + * val_i]' indicate that the number with id 'id_i' has a value equal. to + * 'val_i'. 'nums2[i] = [id_i, val_i]' indicates that the number with the id + * 'id_i' has a value equal to 'val_i'. each array contains unique ids and is + * sorted in ascending order by id. merge the two arrays into one array that is + * sorted in ascending order by id. return the resulting array. the returned + * array must be sorted in ascending order by id. + */ + +int **mergeArrays(int **nums1, int nums1Size, int *nums1ColSize, int **nums2, + int nums2Size, int *nums2ColSize, int *returnSize, + int **returnColumnSizes) { + int **ans = (int **)malloc((nums1Size + nums2Size) * sizeof(int *)); + for (int i = 0; i < nums1Size + nums2Size; i++) + ans[i] = (int *)calloc(2, sizeof(int)); + *returnColumnSizes = (int *)malloc((nums1Size + nums2Size) * sizeof(int)); + for (int i = 0; i < nums1Size + nums2Size; i++) + (*returnColumnSizes)[i] = 2; + *returnSize = 0; + int i = 0, j = 0, k = 0; + bool valid = false; + while (1) { + valid = false; + if (j < nums1Size) + if (i == nums1[j][0]) { + ans[(*returnSize)][0] = i; + ans[(*returnSize)][1] += nums1[j][1]; + valid = true; + j++; + } + if (k < nums2Size) + if (i == nums2[k][0]) { + ans[(*returnSize)][0] = i; + ans[(*returnSize)][1] += nums2[k][1]; + valid = true; + k++; + } + if (valid) + (*returnSize)++; + if (k == nums2Size && j == nums1Size) + break; + i++; + } + return ans; +} + +int main() { + int n11i[3][2] = {{1, 2}, {2, 3}, {4, 5}}, + n21i[3][2] = {{1, 4}, {3, 2}, {4, 1}}, rs1, *rcs1; + int n12i[3][2] = {{2, 4}, {3, 6}, {5, 5}}, n22i[2][2] = {{1, 3}, {4, 3}}, rs2, + *rcs2; + struct two_d_arr n11, n21, n12, n22; + two_d_arr_init(&n11, ARRAY_SIZE(n11i), ARRAY_SIZE(n11i[0]), n11i); + two_d_arr_init(&n21, ARRAY_SIZE(n21i), ARRAY_SIZE(n21i[0]), n21i); + two_d_arr_init(&n12, ARRAY_SIZE(n12i), ARRAY_SIZE(n12i[0]), n12i); + two_d_arr_init(&n22, ARRAY_SIZE(n22i), ARRAY_SIZE(n22i[0]), n22i); + int **ma1 = mergeArrays(n11.arr, n11.row_size, n11.col_size, n21.arr, + n21.row_size, n21.col_size, &rs1, &rcs1); + int **ma2 = mergeArrays(n12.arr, n12.row_size, n12.col_size, n22.arr, + n22.row_size, n22.col_size, &rs2, &rcs2); + for (int i = 0; i < rs1; i++) { + for (int j = 0; j < rcs1[i]; j++) + printf("%d ", ma1[i][j]); // expect: [[1,6],[2,3],[3,2],[4,6]] + printf("\n"); + } + printf("\n"); + for (int i = 0; i < rs2; i++) { + for (int j = 0; j < rcs2[i]; j++) + printf("%d ", ma2[i][j]); // expect: [[1,3],[2,4],[3,6],[4,3],[5,5]] + printf("\n"); + } + printf("\n"); + for (int i = 0; i < rs1; i++) + free(ma1[i]); + free(ma1); + free(rcs1); + for (int i = 0; i < rs2; i++) + free(ma2[i]); + free(ma2); + free(rcs2); + two_d_arr_free(&n11); + two_d_arr_free(&n21); + two_d_arr_free(&n12); + two_d_arr_free(&n22); +} |