diff options
97 files changed, 1315 insertions, 783 deletions
diff --git a/alice_bob_flower_game.c b/alice_bob_flower_game.c new file mode 100644 index 0000000..efe9866 --- /dev/null +++ b/alice_bob_flower_game.c @@ -0,0 +1,17 @@ +// 3021. Alice and Bob Playing Flower Game +#include "leetcode.h" + +/* + * alice and bob are playing a turn-based game on a field with two lanes of + * flowers bet=een them. there are 'x' flowers in the first lane between alice + * and bob, and 'y' flowers in the second lane between them. return the number + * of possible pairs '(x, y)' that satisyf the conditions mentioned in the + * statement. + */ + +long long flowerGame(int n, int m) { return 1LL * n * m / 2; } + +int main() { + printf("%lld\n", flowerGame(3, 2)); // expect: 3 + printf("%lld\n", flowerGame(1, 1)); // expect: 0 +} diff --git a/alice_bob_flower_game.py b/alice_bob_flower_game.py new file mode 100644 index 0000000..96e336d --- /dev/null +++ b/alice_bob_flower_game.py @@ -0,0 +1,25 @@ +# 3021. Alice and Bob Playing Flower Game + +""" +alice and bob are playing a turn-based game on a field with two lanes of +flowers bet=een them. there are 'x' flowers in the first lane between alice +and bob, and 'y' flowers in the second lane between them. return the number +of possible pairs '(x, y)' that satisyf the conditions mentioned in the +statement. +""" + + +class Solution(object): + def flowerGame(self, n, m): + """ + :type n: int + :type m: int + :rtype: int + """ + return n * m // 2 + + +if __name__ == "__main__": + obj = Solution() + print(obj.flowerGame(3, 2)) + print(obj.flowerGame(1, 1)) diff --git a/block_placement_queries.c b/block_placement_queries.c index 3f244ed..358fd95 100644 --- a/block_placement_queries.c +++ b/block_placement_queries.c @@ -137,11 +137,12 @@ int main() { qcs1[i] = ARRAY_SIZE(q1i[i]); for (int i = 0; i < ARRAY_SIZE(q2i); i++) qcs2[i] = ARRAY_SIZE(q2i[i]); - struct two_d_arr q1, q2; - two_d_arr_init(&q1, ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); - two_d_arr_init(&q2, ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); - bool *gr1 = getResults(q1.arr, q1.row_size, qcs1, &rs1); - bool *gr2 = getResults(q2.arr, q2.row_size, qcs2, &rs2); + struct two_d_arr *q1 = + two_d_arr_init(ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); + struct two_d_arr *q2 = + two_d_arr_init(ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); + bool *gr1 = getResults(q1->arr, q1->row_size, qcs1, &rs1); + bool *gr2 = getResults(q2->arr, q2->row_size, qcs2, &rs2); for (int i = 0; i < rs1; i++) printf("%d ", gr1[i]); // expect: 0 1 1 printf("\n"); @@ -150,6 +151,6 @@ int main() { printf("\n"); free(gr1); free(gr2); - two_d_arr_free(&q1); - two_d_arr_free(&q2); + two_d_arr_free(q1); + two_d_arr_free(q2); } diff --git a/brainpower.c b/brainpower.c index 5bff250..c53479f 100644 --- a/brainpower.c +++ b/brainpower.c @@ -27,9 +27,14 @@ long long mostPoints(int **questions, int questionsSize, int main() { int q1i[4][2] = {{3, 2}, {4, 3}, {4, 4}, {2, 5}}, q2i[5][2] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}; - struct two_d_arr q1, q2; - two_d_arr_init(&q1, ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); - two_d_arr_init(&q2, ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); - printf("%lld\n", mostPoints(q1.arr, q1.row_size, q1.col_size)); // expect: 5 - printf("%lld\n", mostPoints(q2.arr, q2.row_size, q2.col_size)); // expect: 7 + struct two_d_arr *q1 = + two_d_arr_init(ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); + struct two_d_arr *q2 = + two_d_arr_init(ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); + printf("%lld\n", + mostPoints(q1->arr, q1->row_size, q1->col_size)); // expect: 5 + printf("%lld\n", + mostPoints(q2->arr, q2->row_size, q2->col_size)); // expect: 7 + two_d_arr_free(q1); + two_d_arr_free(q2); } diff --git a/brick_wall.c b/brick_wall.c index be39306..cb29874 100644 --- a/brick_wall.c +++ b/brick_wall.c @@ -75,11 +75,10 @@ int main() { int w1i[6][4] = {{1, 2, 2, 1}, {3, 1, 2}, {1, 3, 2}, {2, 4}, {3, 1, 2}, {1, 3, 1, 1}}, w2i[3][1] = {{1}, {1}, {1}}; - struct two_d_arr w1, w2; - two_d_arr_init(&w1, 6, 4, w1i); - two_d_arr_init(&w2, 3, 1, w2i); - printf("%d\n", leastBricks(w1.arr, w1.row_size, w1.col_size)); // expect: 2 - printf("%d\n", leastBricks(w2.arr, w2.row_size, w2.col_size)); // expect: 3 - two_d_arr_free(&w1); - two_d_arr_free(&w2); + struct two_d_arr *w1 = two_d_arr_init(6, 4, w1i); + struct two_d_arr *w2 = two_d_arr_init(3, 1, w2i); + printf("%d\n", leastBricks(w1->arr, w1->row_size, w1->col_size)); // expect: 2 + printf("%d\n", leastBricks(w2->arr, w2->row_size, w2->col_size)); // expect: 3 + two_d_arr_free(w1); + two_d_arr_free(w2); } diff --git a/can_make_palindrome_substr.c b/can_make_palindrome_substr.c index 700c5f2..fb71a7a 100644 --- a/can_make_palindrome_substr.c +++ b/can_make_palindrome_substr.c @@ -36,11 +36,12 @@ int main() { char *s1 = "abcda", *s2 = "lyb"; int q1i[5][3] = {{3, 3, 0}, {1, 2, 0}, {0, 3, 1}, {0, 3, 2}, {0, 4, 1}}; int q2i[2][3] = {{0, 1, 0}, {2, 2, 1}}, rs1, rs2; - struct two_d_arr q1, q2; - two_d_arr_init(&q1, 5, 3, q1i); - two_d_arr_init(&q2, 2, 3, q2i); - bool *cmpq1 = canMakePaliQueries(s1, q1.arr, q1.row_size, q1.col_size, &rs1); - bool *cmpq2 = canMakePaliQueries(s2, q2.arr, q2.row_size, q2.col_size, &rs2); + struct two_d_arr *q1 = two_d_arr_init(5, 3, q1i); + struct two_d_arr *q2 = two_d_arr_init(2, 3, q2i); + bool *cmpq1 = + canMakePaliQueries(s1, q1->arr, q1->row_size, q1->col_size, &rs1); + bool *cmpq2 = + canMakePaliQueries(s2, q2->arr, q2->row_size, q2->col_size, &rs2); for (int i = 0; i < rs1; i++) printf("%d ", cmpq1[i]); // expect: 1 0 0 1 1 printf("\n"); @@ -48,5 +49,6 @@ int main() { printf("%d ", cmpq2[i]); // expect: 0 1 printf("\n"); free(cmpq1), free(cmpq2); - two_d_arr_free(&q1), two_d_arr_free(&q2); + two_d_arr_free(q1); + two_d_arr_free(q2); } diff --git a/check_grid_cut_section.c b/check_grid_cut_section.c index 2884222..6c248b0 100644 --- a/check_grid_cut_section.c +++ b/check_grid_cut_section.c @@ -64,17 +64,19 @@ int main() { int r2i[4][4] = {{0, 0, 1, 1}, {2, 0, 3, 4}, {0, 2, 2, 3}, {3, 0, 4, 3}}; int r3i[5][4] = { {0, 2, 2, 4}, {1, 0, 3, 2}, {2, 2, 3, 4}, {3, 0, 4, 2}, {3, 2, 4, 4}}; - struct two_d_arr r1, r2, r3; - two_d_arr_init(&r1, ARRAY_SIZE(r1i), ARRAY_SIZE(r1i[0]), r1i); - two_d_arr_init(&r2, ARRAY_SIZE(r2i), ARRAY_SIZE(r2i[0]), r2i); - two_d_arr_init(&r3, ARRAY_SIZE(r3i), ARRAY_SIZE(r3i[0]), r3i); + struct two_d_arr *r1 = + two_d_arr_init(ARRAY_SIZE(r1i), ARRAY_SIZE(r1i[0]), r1i); + struct two_d_arr *r2 = + two_d_arr_init(ARRAY_SIZE(r2i), ARRAY_SIZE(r2i[0]), r2i); + struct two_d_arr *r3 = + two_d_arr_init(ARRAY_SIZE(r3i), ARRAY_SIZE(r3i[0]), r3i); printf("%d\n", - checkValidCuts(5, r1.arr, r1.row_size, r1.col_size)); // expect: 1 + checkValidCuts(5, r1->arr, r1->row_size, r1->col_size)); // expect: 1 printf("%d\n", - checkValidCuts(4, r2.arr, r2.row_size, r2.col_size)); // expect: 1 + checkValidCuts(4, r2->arr, r2->row_size, r2->col_size)); // expect: 1 printf("%d\n", - checkValidCuts(4, r3.arr, r3.row_size, r3.col_size)); // expect: 0 - two_d_arr_free(&r1); - two_d_arr_free(&r2); - two_d_arr_free(&r3); + checkValidCuts(4, r3->arr, r3->row_size, r3->col_size)); // expect: 0 + two_d_arr_free(r1); + two_d_arr_free(r2); + two_d_arr_free(r3); } diff --git a/count_days_without_meeting.c b/count_days_without_meeting.c index 9e1ff35..6636d13 100644 --- a/count_days_without_meeting.c +++ b/count_days_without_meeting.c @@ -34,14 +34,19 @@ int countDays(int days, int **meetings, int meetingsSize, int main() { int m1i[3][2] = {{5, 7}, {1, 3}, {9, 10}}, m2i[2][2] = {{2, 4}, {1, 3}}, m3i[1][2] = {{1, 6}}; - struct two_d_arr m1, m2, m3; - two_d_arr_init(&m1, ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i); - two_d_arr_init(&m2, ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i); - two_d_arr_init(&m3, ARRAY_SIZE(m3i), ARRAY_SIZE(m3i[0]), m3i); - printf("%d\n", countDays(10, m1.arr, m1.row_size, m1.col_size)); // expect: 2 - printf("%d\n", countDays(5, m2.arr, m2.row_size, m2.col_size)); // expect: 1 - printf("%d\n", countDays(6, m3.arr, m3.row_size, m3.col_size)); // expect: 0 - two_d_arr_free(&m1); - two_d_arr_free(&m2); - two_d_arr_free(&m3); + struct two_d_arr *m1 = + two_d_arr_init(ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i); + struct two_d_arr *m2 = + two_d_arr_init(ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i); + struct two_d_arr *m3 = + two_d_arr_init(ARRAY_SIZE(m3i), ARRAY_SIZE(m3i[0]), m3i); + printf("%d\n", + countDays(10, m1->arr, m1->row_size, m1->col_size)); // expect: 2 + printf("%d\n", + countDays(5, m2->arr, m2->row_size, m2->col_size)); // expect: 1 + printf("%d\n", + countDays(6, m3->arr, m3->row_size, m3->col_size)); // expect: 0 + two_d_arr_free(m1); + two_d_arr_free(m2); + two_d_arr_free(m3); } diff --git a/count_fertile_pyramids_land.c b/count_fertile_pyramids_land.c index 4a0a4b1..c87b05b 100644 --- a/count_fertile_pyramids_land.c +++ b/count_fertile_pyramids_land.c @@ -131,12 +131,16 @@ int main() { int g1i[2][4] = {{0, 1, 1, 0}, {1, 1, 1, 1}}, g2i[3][3] = {{1, 1, 1}, {1, 1, 1}}, g3i[3][5] = {{1, 1, 1, 1, 0}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}}; - struct two_d_arr g1, g2, g3; - two_d_arr_init(&g1, 2, 4, g1i); - two_d_arr_init(&g2, 3, 3, g2i); - two_d_arr_init(&g3, 3, 5, g3i); - printf("%d\n", countPyramids(g1.arr, g1.row_size, g1.col_size)); // expect: 2 - printf("%d\n", countPyramids(g2.arr, g2.row_size, g2.col_size)); // expect: 2 - printf("%d\n", countPyramids(g3.arr, g3.row_size, g3.col_size)); // expect: 13 - two_d_arr_free(&g1), two_d_arr_free(&g2), two_d_arr_free(&g3); + struct two_d_arr *g1 = two_d_arr_init(2, 4, g1i); + struct two_d_arr *g2 = two_d_arr_init(3, 3, g2i); + struct two_d_arr *g3 = two_d_arr_init(3, 5, g3i); + printf("%d\n", + countPyramids(g1->arr, g1->row_size, g1->col_size)); // expect: 2 + printf("%d\n", + countPyramids(g2->arr, g2->row_size, g2->col_size)); // expect: 2 + printf("%d\n", + countPyramids(g3->arr, g3->row_size, g3->col_size)); // expect: 13 + two_d_arr_free(g1); + two_d_arr_free(g2); + two_d_arr_free(g3); } diff --git a/count_num_complete_component.c b/count_num_complete_component.c index a6ede23..8051246 100644 --- a/count_num_complete_component.c +++ b/count_num_complete_component.c @@ -61,13 +61,14 @@ int countCompleteComponents(int n, int **edges, int edgesSize, int main() { int e1i[4][2] = {{0, 1}, {0, 2}, {1, 2}, {3, 4}}, e2i[5][2] = {{0, 1}, {0, 2}, {1, 2}, {3, 4}, {3, 5}}; - struct two_d_arr e1, e2; - two_d_arr_init(&e1, ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); - two_d_arr_init(&e2, ARRAY_SIZE(e2i), ARRAY_SIZE(e2i[0]), e2i); - printf("%d\n", countCompleteComponents(6, e1.arr, e1.row_size, - e1.col_size)); // expect: 3 - printf("%d\n", countCompleteComponents(6, e2.arr, e2.row_size, - e2.col_size)); // expect: 1 - two_d_arr_free(&e1); - two_d_arr_free(&e2); + struct two_d_arr *e1 = + two_d_arr_init(ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); + struct two_d_arr *e2 = + two_d_arr_init(ARRAY_SIZE(e2i), ARRAY_SIZE(e2i[0]), e2i); + printf("%d\n", countCompleteComponents(6, e1->arr, e1->row_size, + e1->col_size)); // expect: 3 + printf("%d\n", countCompleteComponents(6, e2->arr, e2->row_size, + e2->col_size)); // expect: 1 + two_d_arr_free(e1); + two_d_arr_free(e2); } diff --git a/count_num_good_nodes.c b/count_num_good_nodes.c index 44879ed..58e9123 100644 --- a/count_num_good_nodes.c +++ b/count_num_good_nodes.c @@ -64,11 +64,12 @@ int main() { int e1i[6][2] = {{0, 1}, {0, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}}; int e2i[8][2] = {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {0, 5}, {1, 6}, {2, 7}, {3, 8}}; - struct two_d_arr e1, e2; - two_d_arr_init(&e1, 6, 2, e1i); - two_d_arr_init(&e2, 8, 2, e2i); - printf("%d\n", countGoodNodes(e1.arr, e1.row_size, e1.col_size)); // expect: 7 - printf("%d\n", countGoodNodes(e2.arr, e2.row_size, e2.col_size)); // expect: 6 - two_d_arr_free(&e1); - two_d_arr_free(&e2); + struct two_d_arr *e1 = two_d_arr_init(6, 2, e1i); + struct two_d_arr *e2 = two_d_arr_init(8, 2, e2i); + printf("%d\n", + countGoodNodes(e1->arr, e1->row_size, e1->col_size)); // expect: 7 + printf("%d\n", + countGoodNodes(e2->arr, e2->row_size, e2->col_size)); // expect: 6 + two_d_arr_free(e1); + two_d_arr_free(e2); } diff --git a/count_number_root_nodes.c b/count_number_root_nodes.c index ba684ea..93f368d 100644 --- a/count_number_root_nodes.c +++ b/count_number_root_nodes.c @@ -1,5 +1,6 @@ // 2581. Count Number of Possible Root Nodes #include "leetcode.h" +#include <stdio.h> bool check(int *nums, int nums_size, int k) { for (int i = 0; i < nums_size; i++) @@ -88,17 +89,16 @@ int main() { g1i[4][2] = {{1, 3}, {0, 1}, {1, 0}, {2, 4}}; int e2i[4][2] = {{0, 1}, {1, 2}, {2, 3}, {3, 4}}, g2i[4][2] = {{1, 0}, {3, 4}, {2, 1}, {3, 2}}; - struct two_d_arr e1, g1, e2, g2; - two_d_arr_init(&e1, 4, 2, e1i); - two_d_arr_init(&g1, 4, 2, g1i); - two_d_arr_init(&e2, 4, 2, e2i); - two_d_arr_init(&g2, 4, 2, g2i); - printf("%d\n", rootCount(e1.arr, e1.row_size, e1.col_size, g1.arr, - g1.row_size, g1.col_size, 3)); // expect: 3 - printf("%d\n", rootCount(e2.arr, e2.row_size, e2.col_size, g2.arr, - g2.row_size, g2.col_size, 1)); // expect: 5 - two_d_arr_free(&e1); - two_d_arr_free(&g1); - two_d_arr_free(&e2); - two_d_arr_free(&g2); + struct two_d_arr *e1 = two_d_arr_init(4, 2, e1i); + struct two_d_arr *g1 = two_d_arr_init(4, 2, g1i); + struct two_d_arr *e2 = two_d_arr_init(4, 2, e2i); + struct two_d_arr *g2 = two_d_arr_init(4, 2, g2i); + printf("%d\n", rootCount(e1->arr, e1->row_size, e1->col_size, g1->arr, + g1->row_size, g1->col_size, 3)); // expect: 3 + printf("%d\n", rootCount(e2->arr, e2->row_size, e2->col_size, g2->arr, + g2->row_size, g2->col_size, 1)); // expect: 5 + two_d_arr_free(e1); + two_d_arr_free(g1); + two_d_arr_free(e2); + two_d_arr_free(g2); } diff --git a/count_servers_communicate.c b/count_servers_communicate.c index 66d06be..c2f16eb 100644 --- a/count_servers_communicate.c +++ b/count_servers_communicate.c @@ -1,5 +1,6 @@ // 1267. Count Servers that Communicate #include "leetcode.h" +#include <stdio.h> /* * you are given a map of a server centre, represented as a 'm x n' integer @@ -38,14 +39,19 @@ int main() { int g1i[2][2] = {{1, 0}, {0, 1}}; int g2i[2][2] = {{1, 0}, {1, 1}}; int g3i[4][4] = {{1, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; - struct two_d_arr g1, g2, g3; - two_d_arr_init(&g1, ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); - two_d_arr_init(&g2, ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); - two_d_arr_init(&g3, ARRAY_SIZE(g3i), ARRAY_SIZE(g3i[0]), g3i); - printf("%d\n", countServers(g1.arr, g1.row_size, g1.col_size)); // expect: 0 - printf("%d\n", countServers(g2.arr, g2.row_size, g2.col_size)); // expect: 3 - printf("%d\n", countServers(g3.arr, g3.row_size, g3.col_size)); // expect: 4 - two_d_arr_free(&g1); - two_d_arr_free(&g2); - two_d_arr_free(&g3); + struct two_d_arr *g1 = + two_d_arr_init(ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); + struct two_d_arr *g2 = + two_d_arr_init(ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); + struct two_d_arr *g3 = + two_d_arr_init(ARRAY_SIZE(g3i), ARRAY_SIZE(g3i[0]), g3i); + printf("%d\n", countServers(g1->arr, g1->row_size, g1->col_size)); // expect: + // 0 + printf("%d\n", countServers(g2->arr, g2->row_size, g2->col_size)); // expect: + // 3 + printf("%d\n", countServers(g3->arr, g3->row_size, g3->col_size)); // expect: + // 4 + two_d_arr_free(g1); + two_d_arr_free(g2); + two_d_arr_free(g3); } diff --git a/count_square_submatrices_ones.c b/count_square_submatrices_ones.c index 34c8d51..a517ad1 100644 --- a/count_square_submatrices_ones.c +++ b/count_square_submatrices_ones.c @@ -19,11 +19,12 @@ int countSquares(int **matrix, int matrixSize, int *matrixColSize) { int main() { int m1i[3][4] = {{0, 1, 1, 1}, {1, 1, 1, 1}, {0, 1, 1, 1}}, m2i[3][3] = {{1, 0, 1}, {1, 1, 0}, {1, 1, 0}}; - struct two_d_arr m1, m2; - two_d_arr_init(&m1, 4, 3, m1i); - two_d_arr_init(&m2, 3, 3, m2i); - printf("%d\n", countSquares(m1.arr, m1.row_size, m2.col_size)); // expect: 15 - printf("%d\n", countSquares(m2.arr, m2.row_size, m2.col_size)); // expect: 7 - two_d_arr_free(&m1); - two_d_arr_free(&m2); + struct two_d_arr *m1 = two_d_arr_init(4, 3, m1i); + struct two_d_arr *m2 = two_d_arr_init(3, 3, m2i); + printf("%d\n", + countSquares(m1->arr, m1->row_size, m2->col_size)); // expect: 15 + printf("%d\n", + countSquares(m2->arr, m2->row_size, m2->col_size)); // expect: 7 + two_d_arr_free(m1); + two_d_arr_free(m2); } diff --git a/count_submatrices_all_ones.c b/count_submatrices_all_ones.c index ab9c140..184651d 100644 --- a/count_submatrices_all_ones.c +++ b/count_submatrices_all_ones.c @@ -34,11 +34,12 @@ int numSubmat(int **mat, int matSize, int *matColSize) { int main() { int m1i[3][3] = {{1, 0, 1}, {1, 1, 0}, {1, 1, 0}}, m2i[3][4] = {{0, 1, 1, 0}, {0, 1, 1, 1}, {1, 1, 1, 0}}; - struct two_d_arr m1, m2; - two_d_arr_init(&m1, ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i); - two_d_arr_init(&m2, ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i); - printf("%d\n", numSubmat(m1.arr, m1.row_size, m1.col_size)); // expect: 13 - printf("%d\n", numSubmat(m2.arr, m2.row_size, m2.col_size)); // expect: 24 - two_d_arr_free(&m1); - two_d_arr_free(&m2); + struct two_d_arr *m1 = + two_d_arr_init(ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i); + struct two_d_arr *m2 = + two_d_arr_init(ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i); + printf("%d\n", numSubmat(m1->arr, m1->row_size, m1->col_size)); // expect: 13 + printf("%d\n", numSubmat(m2->arr, m2->row_size, m2->col_size)); // expect: 24 + two_d_arr_free(m1); + two_d_arr_free(m2); } diff --git a/count_unguard_cells_grid.c b/count_unguard_cells_grid.c index 17ad819..3b54ebf 100644 --- a/count_unguard_cells_grid.c +++ b/count_unguard_cells_grid.c @@ -1,5 +1,6 @@ // 2257. Count Unguarded Cells in the Grid #include "leetcode.h" +#include <stdio.h> /* * you are given two integers 'm' and 'n' representing a 0-indexed 'm x n' grid. @@ -68,17 +69,18 @@ int main() { int g1i[3][2] = {{0, 0}, {1, 1}, {2, 3}}, w1i[3][2] = {{0, 1}, {2, 2}, {1, 4}}; int g2i[1][2] = {{1, 1}}, w2i[4][2] = {{0, 1}, {1, 0}, {2, 1}, {1, 2}}; - struct two_d_arr g1, w1, g2, w2; - two_d_arr_init(&g1, 3, 2, g1i); - two_d_arr_init(&w1, 3, 2, w1i); - two_d_arr_init(&g2, 1, 2, g2i); - two_d_arr_init(&w2, 4, 2, w2i); - printf("%d\n", countUnguarded(4, 6, g1.arr, g1.row_size, g1.col_size, w1.arr, - w1.row_size, w1.col_size)); // expect: 7 - printf("%d\n", countUnguarded(3, 3, g2.arr, g2.row_size, g2.col_size, w2.arr, - w2.row_size, w2.col_size)); // expect: 4 - two_d_arr_free(&g1); - two_d_arr_free(&g2); - two_d_arr_free(&w1); - two_d_arr_free(&w2); + struct two_d_arr *g1 = two_d_arr_init(3, 2, g1i); + struct two_d_arr *w1 = two_d_arr_init(3, 2, g1i); + struct two_d_arr *g2 = two_d_arr_init(1, 2, g1i); + struct two_d_arr *w2 = two_d_arr_init(4, 2, g1i); + printf("%d\n", + countUnguarded(4, 6, g1->arr, g1->row_size, g1->col_size, w1->arr, + w1->row_size, w1->col_size)); // expect: 7 + printf("%d\n", + countUnguarded(3, 3, g2->arr, g2->row_size, g2->col_size, w2->arr, + w2->row_size, w2->col_size)); // expect: 4 + two_d_arr_free(g1); + two_d_arr_free(g2); + two_d_arr_free(w1); + two_d_arr_free(w2); } diff --git a/count_vowel_string_range.c b/count_vowel_string_range.c index 57eda96..550ce90 100644 --- a/count_vowel_string_range.c +++ b/count_vowel_string_range.c @@ -37,13 +37,12 @@ int main() { char *w1[] = {"aba", "bcb", "ece", "aa", "e"}, *w2[] = {"a", "e", "i"}; int q1i[3][2] = {{0, 2}, {1, 4}, {1, 1}}, rs1; int q2i[3][2] = {{0, 2}, {0, 1}, {2, 2}}, rs2; - struct two_d_arr q1, q2; - two_d_arr_init(&q1, 3, 2, q1i); - two_d_arr_init(&q2, 3, 2, q2i); - int *vs1 = vowelStrings((char **)w1, ARRAY_SIZE(w1), q1.arr, q1.row_size, - q1.col_size, &rs1); - int *vs2 = vowelStrings((char **)w2, ARRAY_SIZE(w2), q2.arr, q2.row_size, - q2.col_size, &rs2); + struct two_d_arr *q1 = two_d_arr_init(3, 2, q1i); + struct two_d_arr *q2 = two_d_arr_init(3, 2, q2i); + int *vs1 = vowelStrings((char **)w1, ARRAY_SIZE(w1), q1->arr, q1->row_size, + q1->col_size, &rs1); + int *vs2 = vowelStrings((char **)w2, ARRAY_SIZE(w2), q2->arr, q2->row_size, + q2->col_size, &rs2); for (int i = 0; i < rs1; i++) printf("%d ", vs1[i]); // expect: 2 3 0 printf("\n"); @@ -51,5 +50,6 @@ int main() { printf("%d ", vs2[i]); // expect: 3 2 1 printf("\n"); free(vs1), free(vs2); - two_d_arr_free(&q1), two_d_arr_free(&q2); + two_d_arr_free(q1); + two_d_arr_free(q2); } diff --git a/course_schedule4.c b/course_schedule4.c index d3f4fff..a695685 100644 --- a/course_schedule4.c +++ b/course_schedule4.c @@ -46,19 +46,23 @@ int main() { int p1i[1][2] = {{1, 0}}, q1i[2][2] = {{0, 1}, {1, 0}}, rs1; int p2i[1][0] = {{}}, q2i[2][2] = {{1, 0}, {0, 1}}, rs2; int p3i[3][2] = {{1, 2}, {1, 0}, {2, 0}}, q3i[2][2] = {{1, 0}, {1, 2}}, rs3; - struct two_d_arr p1, q1, p2, q2, p3, q3; - two_d_arr_init(&p1, ARRAY_SIZE(p1i), ARRAY_SIZE(p1i[0]), p1i); - two_d_arr_init(&p2, 0, ARRAY_SIZE(p2i[0]), p2i); - two_d_arr_init(&p3, ARRAY_SIZE(p3i), ARRAY_SIZE(p3i[0]), p3i); - two_d_arr_init(&q1, ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); - two_d_arr_init(&q2, ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); - two_d_arr_init(&q3, ARRAY_SIZE(q3i), ARRAY_SIZE(q3i[0]), q3i); - bool *cip1 = checkIfPrerequisite(2, p1.arr, p1.row_size, p1.col_size, q1.arr, - q1.row_size, q1.col_size, &rs1); - bool *cip2 = checkIfPrerequisite(2, p2.arr, p2.row_size, p2.col_size, q2.arr, - q2.row_size, q2.col_size, &rs2); - bool *cip3 = checkIfPrerequisite(3, p3.arr, p3.row_size, p3.col_size, q3.arr, - q3.row_size, q3.col_size, &rs3); + struct two_d_arr *p1 = + two_d_arr_init(ARRAY_SIZE(p1i), ARRAY_SIZE(p1i[0]), p1i); + struct two_d_arr *p2 = two_d_arr_init(0, ARRAY_SIZE(p2i[0]), p2i); + struct two_d_arr *p3 = + two_d_arr_init(ARRAY_SIZE(p3i), ARRAY_SIZE(p3i[0]), p3i); + struct two_d_arr *q1 = + two_d_arr_init(ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); + struct two_d_arr *q2 = + two_d_arr_init(ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); + struct two_d_arr *q3 = + two_d_arr_init(ARRAY_SIZE(q3i), ARRAY_SIZE(q3i[0]), q3i); + bool *cip1 = checkIfPrerequisite(2, p1->arr, p1->row_size, p1->col_size, + q1->arr, q1->row_size, q1->col_size, &rs1); + bool *cip2 = checkIfPrerequisite(2, p2->arr, p2->row_size, p2->col_size, + q2->arr, q2->row_size, q2->col_size, &rs2); + bool *cip3 = checkIfPrerequisite(3, p3->arr, p3->row_size, p3->col_size, + q3->arr, q3->row_size, q3->col_size, &rs3); for (int i = 0; i < rs1; i++) printf("%d ", cip1[i]); // expect: 0 1 printf("\n"); @@ -71,10 +75,10 @@ int main() { free(cip1); free(cip2); free(cip3); - two_d_arr_free(&p1); - two_d_arr_free(&p2); - two_d_arr_free(&p3); - two_d_arr_free(&q1); - two_d_arr_free(&q2); - two_d_arr_free(&q3); + two_d_arr_free(p1); + two_d_arr_free(p2); + two_d_arr_free(p3); + two_d_arr_free(q1); + two_d_arr_free(q2); + two_d_arr_free(q3); } diff --git a/cycle_queries_tree.c b/cycle_queries_tree.c index d604e9f..444744d 100644 --- a/cycle_queries_tree.c +++ b/cycle_queries_tree.c @@ -35,11 +35,12 @@ int *cycleLengthQueries(int n, int **queries, int queriesSize, int main() { int q1i[3][2] = {{5, 3}, {4, 7}, {2, 3}}, rs1; int q2i[1][2] = {{1, 2}}, rs2; - struct two_d_arr q1, q2; - two_d_arr_init(&q1, ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); - two_d_arr_init(&q2, ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); - int *clq1 = cycleLengthQueries(3, q1.arr, q1.row_size, q1.col_size, &rs1); - int *clq2 = cycleLengthQueries(2, q2.arr, q2.row_size, q2.col_size, &rs2); + struct two_d_arr *q1 = + two_d_arr_init(ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); + struct two_d_arr *q2 = + two_d_arr_init(ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); + int *clq1 = cycleLengthQueries(3, q1->arr, q1->row_size, q1->col_size, &rs1); + int *clq2 = cycleLengthQueries(2, q2->arr, q2->row_size, q2->col_size, &rs2); for (int i = 0; i < rs1; i++) printf("%d ", clq1[i]); // expect: 4 5 3 printf("\n"); @@ -48,6 +49,6 @@ int main() { printf("\n"); free(clq1); free(clq2); - two_d_arr_free(&q1); - two_d_arr_free(&q2); + two_d_arr_free(q1); + two_d_arr_free(q2); } diff --git a/diagonal_traverse.c b/diagonal_traverse.c index 802271d..43f6316 100644 --- a/diagonal_traverse.c +++ b/diagonal_traverse.c @@ -39,11 +39,12 @@ int *findDiagonalOrder(int **mat, int matSize, int *matColSize, int main() { int m1i[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, m2i[2][2] = {{1, 2}, {3, 4}}; - struct two_d_arr m1, m2; - two_d_arr_init(&m1, ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i); - two_d_arr_init(&m2, ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i); - int rs1, *fdo1 = findDiagonalOrder(m1.arr, m1.row_size, m1.col_size, &rs1); - int rs2, *fdo2 = findDiagonalOrder(m2.arr, m2.row_size, m2.col_size, &rs2); + struct two_d_arr *m1 = + two_d_arr_init(ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i); + struct two_d_arr *m2 = + two_d_arr_init(ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i); + int rs1, *fdo1 = findDiagonalOrder(m1->arr, m1->row_size, m1->col_size, &rs1); + int rs2, *fdo2 = findDiagonalOrder(m2->arr, m2->row_size, m2->col_size, &rs2); for (int i = 0; i < rs1; i++) printf("%d ", fdo1[i]); // expect: 1 2 4 7 5 3 6 8 9 printf("\n"); @@ -52,6 +53,6 @@ int main() { printf("\n"); free(fdo1); free(fdo2); - two_d_arr_free(&m1); - two_d_arr_free(&m2); + two_d_arr_free(m1); + two_d_arr_free(m2); } diff --git a/dungeon_game.c b/dungeon_game.c index 6a54e5d..2bb2543 100644 --- a/dungeon_game.c +++ b/dungeon_game.c @@ -38,13 +38,12 @@ int calculateMinimumHP(int **dungeon, int dungeonSize, int *dungeonColSize) { int main() { int d1i[3][3] = {{-2, -3, 3}, {-5, -10, 1}, {10, 30, -5}}, d2i[1][1] = {{0}}; - struct two_d_arr d1, d2; - two_d_arr_init(&d1, 3, 3, d1i); - two_d_arr_init(&d2, 1, 1, d2i); + struct two_d_arr *d1 = two_d_arr_init(3, 3, d1i); + struct two_d_arr *d2 = two_d_arr_init(1, 1, d2i); printf("%d\n", - calculateMinimumHP(d1.arr, d1.row_size, d1.col_size)); // expect: 7 + calculateMinimumHP(d1->arr, d1->row_size, d1->col_size)); // expect: 7 printf("%d\n", - calculateMinimumHP(d2.arr, d2.row_size, d2.col_size)); // expect: 1 - two_d_arr_free(&d1); - two_d_arr_free(&d2); + calculateMinimumHP(d2->arr, d2->row_size, d2->col_size)); // expect: 1 + two_d_arr_free(d1); + two_d_arr_free(d2); } diff --git a/eventual_safe_states.c b/eventual_safe_states.c index 46059a2..ae862f0 100644 --- a/eventual_safe_states.c +++ b/eventual_safe_states.c @@ -44,11 +44,12 @@ int *eventualSafeNodes(int **graph, int graphSize, int *graphColSize, int main() { int g1i[7][2] = {{1, 2}, {2, 3}, {5}, {0}, {5}, {}, {}}, rs1; int g2i[5][4] = {{1, 2, 3, 4}, {1, 2}, {3, 4}, {0, 4}, {}}, rs2; - struct two_d_arr g1, g2; - two_d_arr_init(&g1, ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); - two_d_arr_init(&g2, ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); - int *esn1 = eventualSafeNodes(g1.arr, g1.row_size, g1.col_size, &rs1); - int *esn2 = eventualSafeNodes(g2.arr, g2.row_size, g2.col_size, &rs2); + struct two_d_arr *g1 = + two_d_arr_init(ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); + struct two_d_arr *g2 = + two_d_arr_init(ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); + int *esn1 = eventualSafeNodes(g1->arr, g1->row_size, g1->col_size, &rs1); + int *esn2 = eventualSafeNodes(g2->arr, g2->row_size, g2->col_size, &rs2); for (int i = 0; i < rs1; i++) printf("%d ", esn1[i]); // expect: 2 4 5 6 printf("\n"); @@ -56,6 +57,6 @@ int main() { printf("%d ", esn2[i]); // expect: 4 printf("\n"); free(esn2), free(esn2); - two_d_arr_free(&g1); - two_d_arr_free(&g2); + two_d_arr_free(g1); + two_d_arr_free(g2); } diff --git a/filter_restaurants_vpd.c b/filter_restaurants_vpd.c index 782af24..01b0a5b 100644 --- a/filter_restaurants_vpd.c +++ b/filter_restaurants_vpd.c @@ -65,16 +65,15 @@ int main() { {3, 8, 1, 30, 4}, {4, 10, 0, 10, 3}, {5, 1, 1, 15, 1}}; - struct two_d_arr r1, r2, r3; - two_d_arr_init(&r1, 5, 5, r1i); - two_d_arr_init(&r2, 5, 5, r2i); - two_d_arr_init(&r3, 5, 5, r3i); - int rs1, *fr1 = filterRestaurants(r1.arr, r1.row_size, r1.col_size, 1, 50, 10, - &rs1); - int rs2, *fr2 = filterRestaurants(r2.arr, r2.row_size, r2.col_size, 0, 50, 10, - &rs2); - int rs3, *fr3 = filterRestaurants(r3.arr, r3.row_size, r3.col_size, 0, 30, 3, - &rs3); + struct two_d_arr *r1 = two_d_arr_init(5, 5, r1i); + struct two_d_arr *r2 = two_d_arr_init(5, 5, r2i); + struct two_d_arr *r3 = two_d_arr_init(5, 5, r3i); + int rs1, *fr1 = filterRestaurants(r1->arr, r1->row_size, r1->col_size, 1, 50, + 10, &rs1); + int rs2, *fr2 = filterRestaurants(r2->arr, r2->row_size, r2->col_size, 0, 50, + 10, &rs2); + int rs3, *fr3 = filterRestaurants(r3->arr, r3->row_size, r3->col_size, 0, 30, + 3, &rs3); for (int i = 0; i < rs1; i++) printf("%d", fr1[i]); // expect: 3 1 5 printf("\n"); @@ -84,7 +83,7 @@ int main() { for (int i = 0; i < rs3; i++) printf("%d", fr3[i]); // expect: 4 5 printf("\n"); - two_d_arr_free(&r1), free(fr1); - two_d_arr_free(&r2), free(fr2); - two_d_arr_free(&r3), free(fr3); + two_d_arr_free(r1), free(fr1); + two_d_arr_free(r2), free(fr2); + two_d_arr_free(r3), free(fr3); } diff --git a/find_champion_2.c b/find_champion_2.c index a6282bb..d25c7b3 100644 --- a/find_champion_2.c +++ b/find_champion_2.c @@ -28,13 +28,13 @@ int findChampion(int n, int **edges, int edgesSize, int *edgesColSize) { int main() { int e1i[2][2] = {{0, 1}, {1, 2}}, e2i[3][2] = {{0, 2}, {1, 3}, {1, 2}}; - struct two_d_arr e1, e2; - two_d_arr_init(&e1, 2, 2, e1i); - two_d_arr_init(&e2, 3, 2, e2i); - printf("%d\n", findChampion(3, e1.arr, e1.row_size, e1.col_size)); // expect: - // 0 + struct two_d_arr *e1 = two_d_arr_init(2, 2, e1i); + struct two_d_arr *e2 = two_d_arr_init(3, 2, e2i); printf("%d\n", - findChampion(4, e2.arr, e2.row_size, e2.col_size)); // expect: -1 - two_d_arr_free(&e1); - two_d_arr_free(&e2); + findChampion(3, e1->arr, e1->row_size, e1->col_size)); // expect: + // 0 + printf("%d\n", + findChampion(4, e2->arr, e2->row_size, e2->col_size)); // expect: -1 + two_d_arr_free(e1); + two_d_arr_free(e2); } diff --git a/find_distinct_ball_colour.c b/find_distinct_ball_colour.c index 6c08491..c542faf 100644 --- a/find_distinct_ball_colour.c +++ b/find_distinct_ball_colour.c @@ -131,11 +131,12 @@ int *queryResults(int limit, int **queries, int queriesSize, int main() { int q1i[4][2] = {{1, 4}, {2, 5}, {1, 3}, {3, 4}}, rs1; int q2i[5][2] = {{0, 1}, {1, 2}, {2, 2}, {3, 4}, {4, 5}}, rs2; - struct two_d_arr q1, q2; - two_d_arr_init(&q1, ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); - two_d_arr_init(&q2, ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); - int *qr1 = queryResults(4, q1.arr, q1.row_size, q1.col_size, &rs1); - int *qr2 = queryResults(4, q2.arr, q2.row_size, q2.col_size, &rs2); + struct two_d_arr *q1 = + two_d_arr_init(ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); + struct two_d_arr *q2 = + two_d_arr_init(ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); + int *qr1 = queryResults(4, q1->arr, q1->row_size, q1->col_size, &rs1); + int *qr2 = queryResults(4, q2->arr, q2->row_size, q2->col_size, &rs2); for (int i = 0; i < rs1; i++) printf("%d ", qr1[i]); // expect: 1 2 2 3 printf("\n"); @@ -144,6 +145,6 @@ int main() { printf("\n"); free(qr1); free(qr2); - two_d_arr_free(&q1); - two_d_arr_free(&q2); + two_d_arr_free(q1); + two_d_arr_free(q2); } diff --git a/find_good_subset_matrix.c b/find_good_subset_matrix.c index aadf5ce..26d704e 100644 --- a/find_good_subset_matrix.c +++ b/find_good_subset_matrix.c @@ -64,16 +64,15 @@ int main() { int g1i[3][4] = {{0, 1, 1, 0}, {0, 0, 0, 1}, {1, 1, 1, 1}}, rs1; int g2i[1][1] = {{0}}, rs2; int g3i[2][3] = {{1, 1, 1}, {1, 1, 1}}, rs3; - struct two_d_arr g1, g2, g3; - two_d_arr_init(&g1, 3, 4, g1i); - two_d_arr_init(&g2, 1, 1, g2i); - two_d_arr_init(&g3, 2, 3, g3i); + struct two_d_arr *g1 = two_d_arr_init(3, 4, g1i); + struct two_d_arr *g2 = two_d_arr_init(1, 1, g2i); + struct two_d_arr *g3 = two_d_arr_init(2, 3, g3i); int *gsobm1 = - goodSubsetofBinaryMatrix(g1.arr, g1.row_size, g1.col_size, &rs1); + goodSubsetofBinaryMatrix(g1->arr, g1->row_size, g1->col_size, &rs1); int *gsobm2 = - goodSubsetofBinaryMatrix(g2.arr, g2.row_size, g2.col_size, &rs2); + goodSubsetofBinaryMatrix(g2->arr, g2->row_size, g2->col_size, &rs2); int *gsobm3 = - goodSubsetofBinaryMatrix(g3.arr, g3.row_size, g3.col_size, &rs3); + goodSubsetofBinaryMatrix(g3->arr, g3->row_size, g3->col_size, &rs3); for (int i = 0; i < rs1; i++) printf("%d ", gsobm1[i]); // expect: 0 1 printf("\n"); @@ -83,7 +82,7 @@ int main() { for (int i = 0; i < rs3; i++) printf("%d ", gsobm3[i]); // expect: null printf("\n"); - free(gsobm1), two_d_arr_free(&g1); - free(gsobm2), two_d_arr_free(&g2); - free(gsobm3), two_d_arr_free(&g3); + free(gsobm1), two_d_arr_free(g1); + free(gsobm2), two_d_arr_free(g2); + free(gsobm3), two_d_arr_free(g3); } diff --git a/find_kth_large_xor_coord.c b/find_kth_large_xor_coord.c index 2611be1..e8bfe81 100644 --- a/find_kth_large_xor_coord.c +++ b/find_kth_large_xor_coord.c @@ -29,14 +29,16 @@ int kthLargestValue(int **matrix, int matrixSize, int *matrixColSize, int k) { int main() { int matrix_input[2][2] = {{5, 2}, {1, 6}}; - struct two_d_arr matrix; - two_d_arr_init(&matrix, ARRAY_SIZE(matrix_input), ARRAY_SIZE(matrix_input[0]), - matrix_input); - printf("%d\n", kthLargestValue(matrix.arr, matrix.row_size, matrix.col_size, - 1)); // expect: 7 - printf("%d\n", kthLargestValue(matrix.arr, matrix.row_size, matrix.col_size, - 2)); // expect: 5 - printf("%d\n", kthLargestValue(matrix.arr, matrix.row_size, matrix.col_size, - 3)); // expect: 4 - two_d_arr_free(&matrix); + struct two_d_arr *matrix = two_d_arr_init( + ARRAY_SIZE(matrix_input), ARRAY_SIZE(matrix_input[0]), matrix_input); + printf("%d\n", + kthLargestValue(matrix->arr, matrix->row_size, matrix->col_size, + 1)); // expect: 7 + printf("%d\n", + kthLargestValue(matrix->arr, matrix->row_size, matrix->col_size, + 2)); // expect: 5 + printf("%d\n", + kthLargestValue(matrix->arr, matrix->row_size, matrix->col_size, + 3)); // expect: 4 + two_d_arr_free(matrix); } diff --git a/find_max_num_fruit_collected.c b/find_max_num_fruit_collected.c index c7226e2..282d9ea 100644 --- a/find_max_num_fruit_collected.c +++ b/find_max_num_fruit_collected.c @@ -47,13 +47,14 @@ int main() { int f1i[4][4] = { {1, 2, 3, 4}, {5, 6, 8, 7}, {9, 10, 11, 12}, {13, 14, 15, 16}}; int f2i[2][2] = {{1, 1}, {1, 1}}; - struct two_d_arr f1, f2; - two_d_arr_init(&f1, ARRAY_SIZE(f1i), ARRAY_SIZE(f1i[0]), f1i); - two_d_arr_init(&f2, ARRAY_SIZE(f2i), ARRAY_SIZE(f2i[0]), f2i); + struct two_d_arr *f1 = + two_d_arr_init(ARRAY_SIZE(f1i), ARRAY_SIZE(f1i[0]), f1i); + struct two_d_arr *f2 = + two_d_arr_init(ARRAY_SIZE(f2i), ARRAY_SIZE(f2i[0]), f2i); + printf("%d\n", maxCollectedFruits(f1->arr, f1->row_size, + f1->col_size)); // expect: 100 printf("%d\n", - maxCollectedFruits(f1.arr, f1.row_size, f1.col_size)); // expect: 100 - printf("%d\n", - maxCollectedFruits(f2.arr, f2.row_size, f2.col_size)); // expect: 4 - two_d_arr_free(&f1); - two_d_arr_free(&f2); + maxCollectedFruits(f2->arr, f2->row_size, f2->col_size)); // expect: 4 + two_d_arr_free(f1); + two_d_arr_free(f2); } diff --git a/find_max_sum_node_val.c b/find_max_sum_node_val.c index 6005bc7..8c5b5bb 100644 --- a/find_max_sum_node_val.c +++ b/find_max_sum_node_val.c @@ -29,17 +29,19 @@ int main() { int n2[] = {2, 3}, e2i[1][2] = {{0, 1}}; int n3[] = {7, 7, 7, 7, 7, 7}, e3i[5][2] = {{0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}}; - struct two_d_arr e1, e2, e3; - two_d_arr_init(&e1, ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); - two_d_arr_init(&e1, ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); - two_d_arr_init(&e1, ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); - printf("%lld\n", maximumValueSum(n1, ARRAY_SIZE(n1), 3, e1.arr, e1.row_size, - e1.col_size)); // expect: 6 - printf("%lld\n", maximumValueSum(n2, ARRAY_SIZE(n2), 7, e2.arr, e2.row_size, - e2.col_size)); // expect: 9 - printf("%lld\n", maximumValueSum(n3, ARRAY_SIZE(n3), 3, e3.arr, e3.row_size, - e3.col_size)); // expect: 42 - two_d_arr_free(&e1); - two_d_arr_free(&e2); - two_d_arr_free(&e3); + struct two_d_arr *e1 = + two_d_arr_init(ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); + struct two_d_arr *e2 = + two_d_arr_init(ARRAY_SIZE(e2i), ARRAY_SIZE(e2i[0]), e2i); + struct two_d_arr *e3 = + two_d_arr_init(ARRAY_SIZE(e3i), ARRAY_SIZE(e3i[0]), e3i); + printf("%lld\n", maximumValueSum(n1, ARRAY_SIZE(n1), 3, e1->arr, e1->row_size, + e1->col_size)); // expect: 6 + printf("%lld\n", maximumValueSum(n2, ARRAY_SIZE(n2), 7, e2->arr, e2->row_size, + e2->col_size)); // expect: 9 + printf("%lld\n", maximumValueSum(n3, ARRAY_SIZE(n3), 3, e3->arr, e3->row_size, + e3->col_size)); // expect: 42 + two_d_arr_free(e1); + two_d_arr_free(e2); + two_d_arr_free(e3); } diff --git a/find_min_area_conver_ones1.c b/find_min_area_conver_ones1.c index 3dc66be..2fabfb1 100644 --- a/find_min_area_conver_ones1.c +++ b/find_min_area_conver_ones1.c @@ -28,11 +28,12 @@ int minimumArea(int **grid, int gridSize, int *gridColSize) { int main() { int g1i[2][3] = {{0, 1, 0}, {1, 0, 1}}, g2i[2][2] = {{1, 0}, {0, 0}}; - struct two_d_arr g1, g2; - two_d_arr_init(&g1, ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); - two_d_arr_init(&g2, ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); - printf("%d\n", minimumArea(g1.arr, g1.row_size, g1.col_size)); // expect: 6 - printf("%d\n", minimumArea(g2.arr, g2.row_size, g2.col_size)); // expect: 1 - two_d_arr_free(&g1); - two_d_arr_free(&g2); + struct two_d_arr *g1 = + two_d_arr_init(ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); + struct two_d_arr *g2 = + two_d_arr_init(ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); + printf("%d\n", minimumArea(g1->arr, g1->row_size, g1->col_size)); // expect: 6 + printf("%d\n", minimumArea(g2->arr, g2->row_size, g2->col_size)); // expect: 1 + two_d_arr_free(g1); + two_d_arr_free(g2); } diff --git a/find_min_area_conver_ones2.c b/find_min_area_conver_ones2.c index 503820c..363210e 100644 --- a/find_min_area_conver_ones2.c +++ b/find_min_area_conver_ones2.c @@ -74,11 +74,12 @@ int minimumSum(int **grid, int gridSize, int *gridColSize) { int main() { int g1i[2][3] = {{1, 0, 1}, {1, 1, 1}}, g2i[2][4] = {{1, 0, 1, 0}, {0, 1, 0, 1}}; - struct two_d_arr g1, g2; - two_d_arr_init(&g1, ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); - two_d_arr_init(&g2, ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); - printf("%d\n", minimumSum(g1.arr, g1.row_size, g1.col_size)); // expect: 5 - printf("%d\n", minimumSum(g2.arr, g2.row_size, g2.col_size)); // expect: 5 - two_d_arr_free(&g1); - two_d_arr_free(&g2); + struct two_d_arr *g1 = + two_d_arr_init(ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); + struct two_d_arr *g2 = + two_d_arr_init(ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); + printf("%d\n", minimumSum(g1->arr, g1->row_size, g1->col_size)); // expect: 5 + printf("%d\n", minimumSum(g2->arr, g2->row_size, g2->col_size)); // expect: 5 + two_d_arr_free(g1); + two_d_arr_free(g2); } diff --git a/find_min_diameter_merge_trees.c b/find_min_diameter_merge_trees.c index a24a927..e236e9d 100644 --- a/find_min_diameter_merge_trees.c +++ b/find_min_diameter_merge_trees.c @@ -91,19 +91,18 @@ int main() { int e1i1[3][2] = {{0, 1}, {0, 2}, {0, 3}}, e2i1[1][2] = {{0, 1}}; int e1i2[7][2] = {{0, 1}, {0, 2}, {0, 3}, {2, 4}, {2, 5}, {3, 6}, {2, 7}}, e2i2[7][2] = {{0, 1}, {0, 2}, {0, 3}, {2, 4}, {2, 5}, {3, 6}, {2, 7}}; - struct two_d_arr e11, e21, e12, e22; - two_d_arr_init(&e11, 3, 2, e1i1); - two_d_arr_init(&e21, 1, 2, e2i1); - two_d_arr_init(&e12, 7, 2, e1i2); - two_d_arr_init(&e22, 7, 2, e2i2); - printf("%d\n", - minimumDiameterAfterMerge(e11.arr, e11.row_size, e11.col_size, e21.arr, - e21.row_size, e21.col_size)); // expect: 3 - printf("%d\n", - minimumDiameterAfterMerge(e12.arr, e12.row_size, e12.col_size, e22.arr, - e22.row_size, e22.col_size)); // expect: 5 - two_d_arr_free(&e11); - two_d_arr_free(&e21); - two_d_arr_free(&e12); - two_d_arr_free(&e22); + struct two_d_arr *e11 = two_d_arr_init(3, 2, e1i1); + struct two_d_arr *e21 = two_d_arr_init(1, 2, e2i1); + struct two_d_arr *e12 = two_d_arr_init(7, 2, e1i2); + struct two_d_arr *e22 = two_d_arr_init(7, 2, e2i2); + printf("%d\n", minimumDiameterAfterMerge( + e11->arr, e11->row_size, e11->col_size, e21->arr, + e21->row_size, e21->col_size)); // expect: 3 + printf("%d\n", minimumDiameterAfterMerge( + e12->arr, e12->row_size, e12->col_size, e22->arr, + e22->row_size, e22->col_size)); // expect: 5 + two_d_arr_free(e11); + two_d_arr_free(e21); + two_d_arr_free(e12); + two_d_arr_free(e22); } diff --git a/find_min_time_reach_last_room1.c b/find_min_time_reach_last_room1.c index e632ae4..668d2b6 100644 --- a/find_min_time_reach_last_room1.c +++ b/find_min_time_reach_last_room1.c @@ -69,17 +69,19 @@ int minTimeToReach(int **moveTime, int moveTimeSize, int *moveTimeColSize) { int main() { int mt1i[2][2] = {{0, 4}, {4, 4}}, mt2i[2][3] = {{0, 0, 0}, {0, 0, 0}}, mt3i[2][2] = {{0, 1}, {1, 2}}; - struct two_d_arr mt1, mt2, mt3; - two_d_arr_init(&mt1, ARRAY_SIZE(mt1i), ARRAY_SIZE(mt1i[0]), mt1i); - two_d_arr_init(&mt2, ARRAY_SIZE(mt2i), ARRAY_SIZE(mt2i[0]), mt2i); - two_d_arr_init(&mt3, ARRAY_SIZE(mt3i), ARRAY_SIZE(mt3i[0]), mt3i); + struct two_d_arr *mt1 = + two_d_arr_init(ARRAY_SIZE(mt1i), ARRAY_SIZE(mt1i[0]), mt1i); + struct two_d_arr *mt2 = + two_d_arr_init(ARRAY_SIZE(mt2i), ARRAY_SIZE(mt2i[0]), mt2i); + struct two_d_arr *mt3 = + two_d_arr_init(ARRAY_SIZE(mt3i), ARRAY_SIZE(mt3i[0]), mt3i); printf("%d\n", - minTimeToReach(mt1.arr, mt1.row_size, mt1.col_size)); // expect: 6 + minTimeToReach(mt1->arr, mt1->row_size, mt1->col_size)); // expect: 6 printf("%d\n", - minTimeToReach(mt2.arr, mt2.row_size, mt2.col_size)); // expect: 3 + minTimeToReach(mt2->arr, mt2->row_size, mt2->col_size)); // expect: 3 printf("%d\n", - minTimeToReach(mt3.arr, mt3.row_size, mt3.col_size)); // expect: 3 - two_d_arr_free(&mt1); - two_d_arr_free(&mt2); - two_d_arr_free(&mt3); + minTimeToReach(mt3->arr, mt3->row_size, mt3->col_size)); // expect: 3 + two_d_arr_free(mt1); + two_d_arr_free(mt2); + two_d_arr_free(mt3); } diff --git a/find_min_time_reach_last_room2.c b/find_min_time_reach_last_room2.c index a81e83a..e3b586a 100644 --- a/find_min_time_reach_last_room2.c +++ b/find_min_time_reach_last_room2.c @@ -134,17 +134,19 @@ int minTimeToReach(int **moveTime, int moveTimeSize, int *moveTimeColSize) { int main() { int mt1i[2][2] = {{0, 4}, {4, 4}}, mt2i[2][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}}, mt3i[2][2] = {{0, 1}, {1, 2}}; - struct two_d_arr mt1, mt2, mt3; - two_d_arr_init(&mt1, ARRAY_SIZE(mt1i), ARRAY_SIZE(mt1i[0]), mt1i); - two_d_arr_init(&mt2, ARRAY_SIZE(mt2i), ARRAY_SIZE(mt2i[0]), mt2i); - two_d_arr_init(&mt3, ARRAY_SIZE(mt3i), ARRAY_SIZE(mt3i[0]), mt3i); + struct two_d_arr *mt1 = + two_d_arr_init(ARRAY_SIZE(mt1i), ARRAY_SIZE(mt1i[0]), mt1i); + struct two_d_arr *mt2 = + two_d_arr_init(ARRAY_SIZE(mt2i), ARRAY_SIZE(mt2i[0]), mt2i); + struct two_d_arr *mt3 = + two_d_arr_init(ARRAY_SIZE(mt3i), ARRAY_SIZE(mt3i[0]), mt3i); printf("%d\n", - minTimeToReach(mt1.arr, mt1.row_size, mt1.col_size)); // expect: 7 + minTimeToReach(mt1->arr, mt1->row_size, mt1->col_size)); // expect: 7 printf("%d\n", - minTimeToReach(mt2.arr, mt2.row_size, mt2.col_size)); // expect: 6 + minTimeToReach(mt2->arr, mt2->row_size, mt2->col_size)); // expect: 6 printf("%d\n", - minTimeToReach(mt3.arr, mt3.row_size, mt3.col_size)); // expect: 4 - two_d_arr_free(&mt1); - two_d_arr_free(&mt2); - two_d_arr_free(&mt3); + minTimeToReach(mt3->arr, mt3->row_size, mt3->col_size)); // expect: 4 + two_d_arr_free(mt1); + two_d_arr_free(mt2); + two_d_arr_free(mt3); } diff --git a/find_missing_repeated_val.c b/find_missing_repeated_val.c index 0514a74..b603396 100644 --- a/find_missing_repeated_val.c +++ b/find_missing_repeated_val.c @@ -35,13 +35,14 @@ int *findMissingAndRepeatedValues(int **grid, int gridSize, int *gridColSize, int main() { int g1i[2][2] = {{1, 3}, {2, 2}}, g2i[3][3] = {{9, 1, 7}, {8, 9, 2}, {3, 4, 6}}; - struct two_d_arr g1, g2; - two_d_arr_init(&g1, ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); - two_d_arr_init(&g2, ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); - int rs1, *fmarv1 = findMissingAndRepeatedValues(g1.arr, g1.row_size, - g1.col_size, &rs1); - int rs2, *fmarv2 = findMissingAndRepeatedValues(g2.arr, g2.row_size, - g2.col_size, &rs2); + struct two_d_arr *g1 = + two_d_arr_init(ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); + struct two_d_arr *g2 = + two_d_arr_init(ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); + int rs1, *fmarv1 = findMissingAndRepeatedValues(g1->arr, g1->row_size, + g1->col_size, &rs1); + int rs2, *fmarv2 = findMissingAndRepeatedValues(g2->arr, g2->row_size, + g2->col_size, &rs2); for (int i = 0; i < rs1; i++) printf("%d ", fmarv1[i]); // expect: 2 4 printf("\n"); @@ -50,6 +51,6 @@ int main() { printf("\n"); free(fmarv1); free(fmarv2); - two_d_arr_free(&g1); - two_d_arr_free(&g2); + two_d_arr_free(g1); + two_d_arr_free(g2); } diff --git a/first_completely_painted_rc.c b/first_completely_painted_rc.c index fc4599b..778d585 100644 --- a/first_completely_painted_rc.c +++ b/first_completely_painted_rc.c @@ -54,13 +54,14 @@ int main() { int a1[] = {1, 3, 4, 2}, m1i[2][2] = {{1, 4}, {2, 3}}; int a2[] = {2, 8, 7, 4, 1, 3, 5, 6, 9}, m2i[3][3] = {{3, 2, 5}, {1, 4, 6}, {8, 7, 9}}; - struct two_d_arr m1, m2; - two_d_arr_init(&m1, ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i); - two_d_arr_init(&m2, ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i); - printf("%d\n", firstCompleteIndex(a1, ARRAY_SIZE(a1), m1.arr, m1.row_size, - m1.col_size)); // expect: 2 - printf("%d\n", firstCompleteIndex(a2, ARRAY_SIZE(a2), m2.arr, m2.row_size, - m2.col_size)); // expect: 3 - two_d_arr_free(&m1); - two_d_arr_free(&m2); + struct two_d_arr *m1 = + two_d_arr_init(ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i); + struct two_d_arr *m2 = + two_d_arr_init(ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i); + printf("%d\n", firstCompleteIndex(a1, ARRAY_SIZE(a1), m1->arr, m1->row_size, + m1->col_size)); // expect: 2 + printf("%d\n", firstCompleteIndex(a2, ARRAY_SIZE(a2), m2->arr, m2->row_size, + m2->col_size)); // expect: 3 + two_d_arr_free(m1); + two_d_arr_free(m2); } diff --git a/flip_col_max_equal_row.c b/flip_col_max_equal_row.c index 9a81f35..741c341 100644 --- a/flip_col_max_equal_row.c +++ b/flip_col_max_equal_row.c @@ -39,17 +39,16 @@ int maxEqualRowsAfterFlips(int **matrix, int matrixSize, int *matrixColSize) { int main() { int m1i[2][2] = {{0, 1}, {1, 1}}, m2i[2][2] = {{0, 1}, {1, 0}}, m3i[3][3] = {{0, 0, 0}, {0, 0, 1}, {1, 1, 0}}; - struct two_d_arr m1, m2, m3; - two_d_arr_init(&m1, 2, 2, m1i); - two_d_arr_init(&m2, 2, 2, m2i); - two_d_arr_init(&m3, 3, 3, m3i); - printf("%d\n", maxEqualRowsAfterFlips(m1.arr, m1.row_size, - m1.col_size)); // expect: 1 - printf("%d\n", maxEqualRowsAfterFlips(m2.arr, m2.row_size, - m2.col_size)); // expect: 2 - printf("%d\n", maxEqualRowsAfterFlips(m3.arr, m3.row_size, - m3.col_size)); // expect: 2 - two_d_arr_free(&m1); - two_d_arr_free(&m2); - two_d_arr_free(&m3); + struct two_d_arr *m1 = two_d_arr_init(2, 2, m1i); + struct two_d_arr *m2 = two_d_arr_init(2, 2, m2i); + struct two_d_arr *m3 = two_d_arr_init(3, 3, m3i); + printf("%d\n", maxEqualRowsAfterFlips(m1->arr, m1->row_size, + m1->col_size)); // expect: 1 + printf("%d\n", maxEqualRowsAfterFlips(m2->arr, m2->row_size, + m2->col_size)); // expect: 2 + printf("%d\n", maxEqualRowsAfterFlips(m3->arr, m3->row_size, + m3->col_size)); // expect: 2 + two_d_arr_free(m1); + two_d_arr_free(m2); + two_d_arr_free(m3); } diff --git a/flood_fill.c b/flood_fill.c index f01f66d..d0ee7b2 100644 --- a/flood_fill.c +++ b/flood_fill.c @@ -39,12 +39,15 @@ int **floodFill(int **image, int imageSize, int *imageColSize, int sr, int sc, int main() { int i1i[3][3] = {{1, 1, 1}, {1, 1, 0}, {1, 0, 1}}, i2i[2][3] = {{0, 0, 0}, {0, 0, 0}}; - struct two_d_arr i1, i2; - two_d_arr_init(&i1, ARRAY_SIZE(i1i), ARRAY_SIZE(i1i[0]), i1i); - two_d_arr_init(&i2, ARRAY_SIZE(i2i), ARRAY_SIZE(i2i[0]), i2i); + struct two_d_arr *i1 = + two_d_arr_init(ARRAY_SIZE(i1i), ARRAY_SIZE(i1i[0]), i1i); + struct two_d_arr *i2 = + two_d_arr_init(ARRAY_SIZE(i2i), ARRAY_SIZE(i2i[0]), i2i); int rs1, rs2, *rcs1, *rcs2; - int **ff1 = floodFill(i1.arr, i1.row_size, i1.col_size, 1, 1, 2, &rs1, &rcs1); - int **ff2 = floodFill(i2.arr, i2.row_size, i2.col_size, 0, 0, 0, &rs2, &rcs2); + int **ff1 = + floodFill(i1->arr, i1->row_size, i1->col_size, 1, 1, 2, &rs1, &rcs1); + int **ff2 = + floodFill(i2->arr, i2->row_size, i2->col_size, 0, 0, 0, &rs2, &rcs2); for (int i = 0; i < rs1; i++) { for (int j = 0; j < rcs1[i]; j++) printf("%d ", ff1[i][j]); // expect: [[2,2,2],[2,2,0],[2,0,1]] @@ -62,5 +65,8 @@ int main() { free(ff1), free(rcs1); for (int i = 0; i < rs2; i++) free(ff2[i]); - free(ff2), free(rcs2); + free(ff2); + free(rcs2); + two_d_arr_free(i1); + two_d_arr_free(i2); } diff --git a/grid_game.c b/grid_game.c index 1945c92..88d69a1 100644 --- a/grid_game.c +++ b/grid_game.c @@ -32,14 +32,16 @@ int main() { int g1i[2][3] = {{2, 5, 4}, {1, 5, 1}}; int g2i[2][3] = {{3, 3, 1}, {8, 5, 2}}; int g3i[2][4] = {{1, 3, 1, 15}, {1, 3, 3, 1}}; - struct two_d_arr g1, g2, g3; - two_d_arr_init(&g1, ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); - two_d_arr_init(&g2, ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); - two_d_arr_init(&g3, ARRAY_SIZE(g3i), ARRAY_SIZE(g3i[0]), g3i); - printf("%lld\n", gridGame(g1.arr, g1.row_size, g1.col_size)); // expect: 4 - printf("%lld\n", gridGame(g2.arr, g2.row_size, g2.col_size)); // expect: 4 - printf("%lld\n", gridGame(g3.arr, g3.row_size, g3.col_size)); // expect: 7 - two_d_arr_free(&g1); - two_d_arr_free(&g2); - two_d_arr_free(&g3); + struct two_d_arr *g1 = + two_d_arr_init(ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); + struct two_d_arr *g2 = + two_d_arr_init(ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); + struct two_d_arr *g3 = + two_d_arr_init(ARRAY_SIZE(g3i), ARRAY_SIZE(g3i[0]), g3i); + printf("%lld\n", gridGame(g1->arr, g1->row_size, g1->col_size)); // expect: 4 + printf("%lld\n", gridGame(g2->arr, g2->row_size, g2->col_size)); // expect: 4 + printf("%lld\n", gridGame(g3->arr, g3->row_size, g3->col_size)); // expect: 7 + two_d_arr_free(g1); + two_d_arr_free(g2); + two_d_arr_free(g3); } diff --git a/k_closest_points_origin.c b/k_closest_points_origin.c index b0c2a51..c8b5693 100644 --- a/k_closest_points_origin.c +++ b/k_closest_points_origin.c @@ -88,12 +88,13 @@ int **kClosest(int **points, int pointsSize, int *pointsColSize, int k, int main() { int p1i[2][2] = {{1, 3}, {-2, 2}}, p2i[3][2] = {{3, 3}, {5, -1}, {-2, 4}}; - struct two_d_arr p1, p2; - two_d_arr_init(&p1, ARRAY_SIZE(p1i), ARRAY_SIZE(p1i[0]), p1i); - two_d_arr_init(&p2, ARRAY_SIZE(p2i), ARRAY_SIZE(p2i[0]), p2i); + struct two_d_arr *p1 = + two_d_arr_init(ARRAY_SIZE(p1i), ARRAY_SIZE(p1i[0]), p1i); + struct two_d_arr *p2 = + two_d_arr_init(ARRAY_SIZE(p2i), ARRAY_SIZE(p2i[0]), p2i); int rs1, rs2, *rcs1, *rcs2; - int **kc1 = kClosest(p1.arr, p1.row_size, p1.col_size, 1, &rs1, &rcs1); - int **kc2 = kClosest(p2.arr, p2.row_size, p2.col_size, 2, &rs2, &rcs2); + int **kc1 = kClosest(p1->arr, p1->row_size, p1->col_size, 1, &rs1, &rcs1); + int **kc2 = kClosest(p2->arr, p2->row_size, p2->col_size, 2, &rs2, &rcs2); for (int i = 0; i < rs1; i++) { for (int j = 0; j < rcs1[i]; j++) printf("%d ", kc1[i][j]); // expect: [[-2,2]] @@ -110,10 +111,10 @@ int main() { free(kc1); free(kc1); free(rcs1); - two_d_arr_free(&p1); + two_d_arr_free(p1); for (int i = 0; i < rs2; i++) free(kc2); free(kc2); free(rcs2); - two_d_arr_free(&p2); + two_d_arr_free(p2); } diff --git a/largest_colour_val_dir_graph.c b/largest_colour_val_dir_graph.c index 79771bf..602fd48 100644 --- a/largest_colour_val_dir_graph.c +++ b/largest_colour_val_dir_graph.c @@ -105,11 +105,14 @@ int largestPathValue(char *colors, int **edges, int edgesSize, int main() { char *c1 = "abaca", *c2 = "a"; int e1i[4][2] = {{0, 1}, {0, 2}, {2, 3}, {3, 4}}, e2i[1][2] = {{0, 0}}; - struct two_d_arr e1, e2; - two_d_arr_init(&e1, ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); - two_d_arr_init(&e2, ARRAY_SIZE(e2i), ARRAY_SIZE(e2i[0]), e2i); - printf("%d\n", - largestPathValue(c1, e1.arr, e1.row_size, e1.col_size)); // expect: 3 - printf("%d\n", - largestPathValue(c2, e2.arr, e2.row_size, e2.col_size)); // expect: -1 + struct two_d_arr *e1 = + two_d_arr_init(ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); + struct two_d_arr *e2 = + two_d_arr_init(ARRAY_SIZE(e2i), ARRAY_SIZE(e2i[0]), e2i); + printf("%d\n", largestPathValue(c1, e1->arr, e1->row_size, + e1->col_size)); // expect: 3 + printf("%d\n", largestPathValue(c2, e2->arr, e2->row_size, + e2->col_size)); // expect: -1 + two_d_arr_free(e1); + two_d_arr_free(e2); } @@ -1,16 +1,17 @@ #include "leetcode.h" -void two_d_arr_init(struct two_d_arr *obj, int row_size, int col_size, - int arr[row_size][col_size]) { +struct two_d_arr *two_d_arr_init(int row_size, int col_size, + int arr[][col_size]) { + struct two_d_arr *obj = (struct two_d_arr *)malloc(sizeof(struct two_d_arr)); obj->row_size = row_size; obj->col_size = (int *)malloc(row_size * sizeof(int)); obj->arr = (int **)malloc(row_size * sizeof(int *)); for (int i = 0; i < row_size; i++) { obj->col_size[i] = col_size; obj->arr[i] = (int *)malloc(col_size * sizeof(int)); - for (int j = 0; j < col_size; j++) - obj->arr[i][j] = arr[i][j]; + memcpy(obj->arr[i], arr[i], col_size * sizeof(int)); } + return obj; } void two_d_arr_free(struct two_d_arr *obj) { @@ -35,8 +35,8 @@ struct Node { struct Node **children; }; -void two_d_arr_init(struct two_d_arr *obj, int row_size, int col_size, - int arr[row_size][col_size]); +struct two_d_arr *two_d_arr_init(int row_size, int col_size, + int arr[row_size][col_size]); void two_d_arr_free(struct two_d_arr *obj); struct ListNode *listnode_create(int val); diff --git a/length_longest_vshape_diag_seg.c b/length_longest_vshape_diag_seg.c new file mode 100644 index 0000000..a30c617 --- /dev/null +++ b/length_longest_vshape_diag_seg.c @@ -0,0 +1,91 @@ +// 3459. Length of Longest V-Shaped Diagonal Segment +#include "leetcode.h" + +/* + * you are given a 2d integer matrix 'grid' of size 'n x m', where each element + * is either 0, 1, or 2. a v-shaped diagonal segment is defined as: the segment + * starts with 1, the subsequent elements follow this infinite sequence '2, 0, + * 2, 0, ...', the segment starts along a diagonal direction, the segment + * continues the sequence in the same diagonal direction, and the segment makes + * at most one clockwise 90-degree turn to another diagonal direction while + * maintaining the sequence. + */ + +const static int dx[] = {1, 1, -1, -1}, dy[] = {1, -1, -1, 1}; +int dp[501][501][4][2]; + +bool pos(int **grid, int gridSize, int *gridColSize, int x, int y, int nx, + int ny) { + return nx >= 0 && ny >= 0 && nx < gridSize && ny < gridColSize[0] && + abs(grid[x][y] - grid[nx][ny]) == 2; +} + +int dfs(int **grid, int gridSize, int *gridColSize, int x, int y, int d, + int changed) { + if (dp[x][y][d][changed] != -1) + return dp[x][y][d][changed]; + int res = 1; + if (pos(grid, gridSize, gridColSize, x, y, x + dx[d], y + dy[d])) + res = fmax( + res, + dfs(grid, gridSize, gridColSize, x + dx[d], y + dy[d], d, changed) + 1); + if (!changed) { + int nd = (d + 1) % 4; + if (pos(grid, gridSize, gridColSize, x, y, x + dx[nd], y + dy[nd])) + res = fmax(res, dfs(grid, gridSize, gridColSize, x + dx[nd], y + dy[nd], + nd, changed + 1) + + 1); + } + return dp[x][y][d][changed] = res; +} + +int lenOfVDiagonal(int **grid, int gridSize, int *gridColSize) { + const int n = gridSize, m = gridColSize[0]; + int ans = 0; + memset(dp, -1, sizeof(dp)); + for (int i = 0; i < gridSize; ++i) + for (int j = 0; j < gridColSize[0]; ++j) + if (grid[i][j] == 1) { + ans = fmax(ans, 1); + for (int k = 0; k < 4; ++k) { + int nx = i + dx[k], ny = j + dy[k]; + if (nx < 0 || ny < 0 || nx >= n || ny >= m || grid[nx][ny] != 2) + continue; + ans = fmax(ans, dfs(grid, gridSize, gridColSize, nx, ny, k, 0) + 1); + } + } + return ans; +} + +int main() { + int g1i[5][5] = {{2, 2, 1, 2, 2}, + {2, 0, 2, 2, 0}, + {2, 0, 1, 1, 0}, + {1, 0, 2, 2, 2}, + {2, 0, 0, 2, 2}}; + int g2i[5][5] = {{2, 2, 2, 2, 2}, + {2, 0, 2, 2, 0}, + {2, 0, 1, 1, 0}, + {1, 0, 2, 2, 2}, + {2, 0, 0, 2, 2}}; + int g3i[5][5] = {{1, 2, 2, 2, 2}, + {2, 2, 2, 2, 0}, + {2, 0, 0, 0, 0}, + {0, 0, 2, 2, 2}, + {2, 0, 0, 2, 0}}; + struct two_d_arr *g1 = + two_d_arr_init(ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); + struct two_d_arr *g2 = + two_d_arr_init(ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); + struct two_d_arr *g3 = + two_d_arr_init(ARRAY_SIZE(g3i), ARRAY_SIZE(g3i[0]), g3i); + printf("%d\n", + lenOfVDiagonal(g1->arr, g1->row_size, g1->col_size)); // expect: 5 + printf("%d\n", + lenOfVDiagonal(g2->arr, g2->row_size, g2->col_size)); // expect: 4 + printf("%d\n", + lenOfVDiagonal(g3->arr, g3->row_size, g3->col_size)); // expect: 5 + two_d_arr_free(g1); + two_d_arr_free(g2); + two_d_arr_free(g3); +} diff --git a/length_longest_vshape_diag_seg.py b/length_longest_vshape_diag_seg.py new file mode 100644 index 0000000..bd7c875 --- /dev/null +++ b/length_longest_vshape_diag_seg.py @@ -0,0 +1,78 @@ +# 3459. Length of Longest V-Shaped Diagonal Segment + +""" +you are given a 2d integer matrix 'grid' of size 'n x m', where each element +is either 0, 1, or 2. a v-shaped diagonal segment is defined as: the segment +starts with 1, the subsequent elements follow this infinite sequence '2, 0, +2, 0, ...', the segment starts along a diagonal direction, the segment +continues the sequence in the same diagonal direction, and the segment makes +at most one clockwise 90-degree turn to another diagonal direction while +maintaining the sequence. +""" + + +class Solution(object): + def lenOfVDiagonal(self, grid): + """ + :type grid: List[List[int]] + :rtype: int + """ + ds = [[1, 1], [1, -1], [-1, -1], [-1, 1]] + nx = [2, 2, 0] + ans, n, m = 0, len(grid), len(grid[0]) + + def dp(i, j, x, d, k): + if not (0 <= i < n and 0 <= j < m): + return 0 + if grid[i][j] != x: + return 0 + res = dp(i + ds[d][0], j + ds[d][1], nx[x], d, k) + 1 + if k > 0: + d2 = (d + 1) % 4 + res2 = dp(i + ds[d2][0], j + ds[d2][1], nx[x], d2, 0) + 1 + res = max(res, res2) + return res + + for i in range(n): + for j in range(m): + if grid[i][j] == 1: + curr = max(dp(i, j, 1, d, 1) for d in range(4)) + ans = max(ans, curr) + return ans + + +if __name__ == "__main__": + obj = Solution() + print( + obj.lenOfVDiagonal( + grid=[ + [2, 2, 1, 2, 2], + [2, 0, 2, 2, 0], + [2, 0, 1, 1, 0], + [1, 0, 2, 2, 2], + [2, 0, 0, 2, 2], + ] + ) + ) + print( + obj.lenOfVDiagonal( + grid=[ + [2, 2, 2, 2, 2], + [2, 0, 2, 2, 0], + [2, 0, 1, 1, 0], + [1, 0, 2, 2, 2], + [2, 0, 0, 2, 2], + ] + ) + ) + print( + obj.lenOfVDiagonal( + grid=[ + [1, 2, 2, 2, 2], + [2, 2, 2, 2, 0], + [2, 0, 0, 0, 0], + [0, 0, 2, 2, 2], + [2, 0, 0, 2, 0], + ] + ) + ) diff --git a/making_large_island.c b/making_large_island.c index 49e6e60..79e6c72 100644 --- a/making_large_island.c +++ b/making_large_island.c @@ -84,14 +84,19 @@ int largestIsland(int **grid, int gridSize, int *gridColSize) { int main() { int g1i[2][2] = {{1, 0}, {0, 1}}, g2i[2][2] = {{1, 1}, {1, 0}}, g3i[2][2] = {{1, 1}, {1, 1}}; - struct two_d_arr g1, g2, g3; - two_d_arr_init(&g1, ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); - two_d_arr_init(&g2, ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); - two_d_arr_init(&g3, ARRAY_SIZE(g3i), ARRAY_SIZE(g3i[0]), g3i); - printf("%d\n", largestIsland(g1.arr, g1.row_size, g1.col_size)); // expect: 3 - printf("%d\n", largestIsland(g2.arr, g2.row_size, g2.col_size)); // expect: 4 - printf("%d\n", largestIsland(g3.arr, g3.row_size, g3.col_size)); // expect: 4 - two_d_arr_free(&g1); - two_d_arr_free(&g2); - two_d_arr_free(&g3); + struct two_d_arr *g1 = + two_d_arr_init(ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); + struct two_d_arr *g2 = + two_d_arr_init(ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); + struct two_d_arr *g3 = + two_d_arr_init(ARRAY_SIZE(g3i), ARRAY_SIZE(g3i[0]), g3i); + printf("%d\n", + largestIsland(g1->arr, g1->row_size, g1->col_size)); // expect: 3 + printf("%d\n", + largestIsland(g2->arr, g2->row_size, g2->col_size)); // expect: 4 + printf("%d\n", + largestIsland(g3->arr, g3->row_size, g3->col_size)); // expect: 4 + two_d_arr_free(g1); + two_d_arr_free(g2); + two_d_arr_free(g3); } diff --git a/map_highest_peak.c b/map_highest_peak.c index d98e563..e9e143b 100644 --- a/map_highest_peak.c +++ b/map_highest_peak.c @@ -83,12 +83,13 @@ int **highestPeak(int **isWater, int isWaterSize, int *isWaterColSize, int main() { int iw1i[2][2] = {{0, 1}, {0, 0}}, iw2i[3][3] = {{0, 0, 1}, {1, 0, 0}, {0, 0, 0}}; - struct two_d_arr iw1, iw2; - two_d_arr_init(&iw1, ARRAY_SIZE(iw1i), ARRAY_SIZE(iw1i[0]), iw1i); - two_d_arr_init(&iw2, ARRAY_SIZE(iw2i), ARRAY_SIZE(iw2i[0]), iw2i); + struct two_d_arr *iw1 = + two_d_arr_init(ARRAY_SIZE(iw1i), ARRAY_SIZE(iw1i[0]), iw1i); + struct two_d_arr *iw2 = + two_d_arr_init(ARRAY_SIZE(iw2i), ARRAY_SIZE(iw2i[0]), iw2i); int rs1, rs2, *rcs1, *rcs2; - int **hp1 = highestPeak(iw1.arr, iw1.row_size, iw1.col_size, &rs1, &rcs1); - int **hp2 = highestPeak(iw2.arr, iw2.row_size, iw2.col_size, &rs2, &rcs2); + int **hp1 = highestPeak(iw1->arr, iw1->row_size, iw1->col_size, &rs1, &rcs1); + int **hp2 = highestPeak(iw2->arr, iw2->row_size, iw2->col_size, &rs2, &rcs2); for (int i = 0; i < rs1; i++) { for (int j = 0; j < rcs1[i]; j++) printf("%d ", hp1[i][j]); // expect: 1 0 2 1 @@ -105,10 +106,10 @@ int main() { free(hp1[i]); free(hp1); free(rcs1); - two_d_arr_free(&iw1); + two_d_arr_free(iw1); for (int i = 0; i < rs2; i++) free(hp2[i]); free(hp2); free(rcs2); - two_d_arr_free(&iw2); + two_d_arr_free(iw2); } diff --git a/max_area_longest_diag_rect.c b/max_area_longest_diag_rect.c new file mode 100644 index 0000000..78ec1e1 --- /dev/null +++ b/max_area_longest_diag_rect.c @@ -0,0 +1,38 @@ +// 3000. Maximum Area of Longest Diagonal Rectangle +#include "leetcode.h" + +/* + * you are given a 2d 0-indexed integer array 'dimensions'. for all indices 'i', + * '0 <= i < dimensions.size', 'dimensions[i][0]' represents the length and + * 'dimensions[i][1]' represents the width of the rectangle 'i'. return the area + * of the rectangle having the longest diagonal, return the area of the + * rectangle having the maximum area. + */ + +int areaOfMaxDiagonal(int **dimensions, int dimensionsSize, + int *dimensionsColSize) { + int ans = 0, max = 0; + for (int i = 0; i < dimensionsSize; i++) { + int l = dimensions[i][0], w = dimensions[i][1]; + int curr = l * l + w * w; + if (curr > max || (curr == max && l * w > ans)) { + max = curr; + ans = l * w; + } + } + return ans; +} + +int main() { + int d1i[2][2] = {{9, 3}, {8, 6}}, d2i[2][2] = {{3, 4}, {4, 3}}; + struct two_d_arr *d1 = + two_d_arr_init(ARRAY_SIZE(d1i), ARRAY_SIZE(d1i[0]), d1i); + struct two_d_arr *d2 = + two_d_arr_init(ARRAY_SIZE(d2i), ARRAY_SIZE(d2i[0]), d2i); + printf("%d\n", + areaOfMaxDiagonal(d1->arr, d1->row_size, d1->col_size)); // expect: 48 + printf("%d\n", + areaOfMaxDiagonal(d2->arr, d2->row_size, d2->col_size)); // expect: 12 + two_d_arr_free(d1); + two_d_arr_free(d2); +} diff --git a/max_area_longest_diag_rect.py b/max_area_longest_diag_rect.py new file mode 100644 index 0000000..7789f74 --- /dev/null +++ b/max_area_longest_diag_rect.py @@ -0,0 +1,30 @@ +# 3000. Maximum Area of Longest Diagonal Rectangle + +""" +you are given a 2d 0-indexed integer array 'dimensions'. for all indices 'i', +'0 <= i < dimensions.size', 'dimensions[i][0]' represents the length and +'dimensions[i][1]' represents the width of the rectangle 'i'. return the area +of the rectangle having the longest diagonal, return the area of the +rectangle having the maximum area. +""" + + +class Solution(object): + def areaOfMaxDiagonal(self, dimensions): + """ + :type dimensions: List[List[int]] + :rtype: int + """ + ans, area = 0, 0 + for l, w in dimensions: + curr = l * l + w * w + if curr > area or (curr == area and l * w > ans): + area = curr + ans = l * w + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.areaOfMaxDiagonal(dimensions=[[9, 3], [8, 6]])) + print(obj.areaOfMaxDiagonal(dimensions=[[3, 4], [4, 3]])) diff --git a/max_candies_from_boxes.c b/max_candies_from_boxes.c index 03b4b5f..d1d4633 100644 --- a/max_candies_from_boxes.c +++ b/max_candies_from_boxes.c @@ -66,19 +66,22 @@ int main() { int s2[] = {1, 0, 0, 0, 0, 0}, c2[] = {1, 1, 1, 1, 1, 1}, k2i[6][5] = {{1, 2, 3, 4, 5}, {}, {}, {}, {}, {}}, cb2i[6][5] = {{1, 2, 3, 4, 5}, {}, {}, {}, {}, {}}, ib2[] = {0}; - struct two_d_arr k1, k2, cb1, cb2; - two_d_arr_init(&k1, ARRAY_SIZE(k1i), ARRAY_SIZE(k1i[0]), k1i); - two_d_arr_init(&k2, ARRAY_SIZE(k2i), ARRAY_SIZE(k2i[0]), k2i); - two_d_arr_init(&cb1, ARRAY_SIZE(cb1i), ARRAY_SIZE(cb1i[0]), cb1i); - two_d_arr_init(&cb2, ARRAY_SIZE(cb2i), ARRAY_SIZE(cb2i[0]), cb2i); - printf("%d\n", maxCandies(s1, ARRAY_SIZE(s1), c1, ARRAY_SIZE(c1), k1.arr, - k1.row_size, k1.col_size, cb1.arr, cb1.row_size, - cb1.col_size, ib1, ARRAY_SIZE(ib1))); // expect: 16 - printf("%d\n", maxCandies(s2, ARRAY_SIZE(s2), c2, ARRAY_SIZE(c2), k2.arr, - k2.row_size, k2.col_size, cb2.arr, cb2.row_size, - cb2.col_size, ib2, ARRAY_SIZE(ib2))); // expect: 6 - two_d_arr_free(&k1); - two_d_arr_free(&k2); - two_d_arr_free(&cb1); - two_d_arr_free(&cb2); + struct two_d_arr *k1 = + two_d_arr_init(ARRAY_SIZE(k1i), ARRAY_SIZE(k1i[0]), k1i); + struct two_d_arr *k2 = + two_d_arr_init(ARRAY_SIZE(k2i), ARRAY_SIZE(k2i[0]), k2i); + struct two_d_arr *cb1 = + two_d_arr_init(ARRAY_SIZE(cb1i), ARRAY_SIZE(cb1i[0]), cb1i); + struct two_d_arr *cb2 = + two_d_arr_init(ARRAY_SIZE(cb2i), ARRAY_SIZE(cb2i[0]), cb2i); + printf("%d\n", maxCandies(s1, ARRAY_SIZE(s1), c1, ARRAY_SIZE(c1), k1->arr, + k1->row_size, k1->col_size, cb1->arr, cb1->row_size, + cb1->col_size, ib1, ARRAY_SIZE(ib1))); // expect: 16 + printf("%d\n", maxCandies(s2, ARRAY_SIZE(s2), c2, ARRAY_SIZE(c2), k2->arr, + k2->row_size, k2->col_size, cb2->arr, cb2->row_size, + cb2->col_size, ib2, ARRAY_SIZE(ib2))); // expect: 6 + two_d_arr_free(k1); + two_d_arr_free(k2); + two_d_arr_free(cb1); + two_d_arr_free(cb2); } diff --git a/max_fruits_harvested_k_step.c b/max_fruits_harvested_k_step.c index 47776de..e0abcbe 100644 --- a/max_fruits_harvested_k_step.c +++ b/max_fruits_harvested_k_step.c @@ -126,17 +126,19 @@ int main() { int f1i[3][2] = {{2, 8}, {6, 3}, {8, 6}}, f2i[6][2] = {{0, 9}, {4, 1}, {5, 7}, {6, 2}, {7, 4}, {10, 9}}, f3i[3][2] = {{0, 3}, {6, 4}, {8, 5}}; - struct two_d_arr f1, f2, f3; - two_d_arr_init(&f1, ARRAY_SIZE(f1i), ARRAY_SIZE(f1i[0]), f1i); - two_d_arr_init(&f2, ARRAY_SIZE(f2i), ARRAY_SIZE(f2i[0]), f2i); - two_d_arr_init(&f3, ARRAY_SIZE(f3i), ARRAY_SIZE(f3i[0]), f3i); - printf("%d\n", - maxTotalFruits(f1.arr, f1.row_size, f1.col_size, 5, 4)); // expect: 9 - printf("%d\n", - maxTotalFruits(f2.arr, f2.row_size, f2.col_size, 5, 4)); // expect: 14 - printf("%d\n", - maxTotalFruits(f3.arr, f3.row_size, f3.col_size, 3, 2)); // expect: 0 - two_d_arr_free(&f1); - two_d_arr_free(&f2); - two_d_arr_free(&f3); + struct two_d_arr *f1 = + two_d_arr_init(ARRAY_SIZE(f1i), ARRAY_SIZE(f1i[0]), f1i); + struct two_d_arr *f2 = + two_d_arr_init(ARRAY_SIZE(f2i), ARRAY_SIZE(f2i[0]), f2i); + struct two_d_arr *f3 = + two_d_arr_init(ARRAY_SIZE(f3i), ARRAY_SIZE(f3i[0]), f3i); + printf("%d\n", maxTotalFruits(f1->arr, f1->row_size, f1->col_size, 5, + 4)); // expect: 9 + printf("%d\n", maxTotalFruits(f2->arr, f2->row_size, f2->col_size, 5, + 4)); // expect: 14 + printf("%d\n", maxTotalFruits(f3->arr, f3->row_size, f3->col_size, 3, + 2)); // expect: 0 + two_d_arr_free(f1); + two_d_arr_free(f2); + two_d_arr_free(f3); } diff --git a/max_height_stacking_cuboids.c b/max_height_stacking_cuboids.c index 22417a4..2562b8c 100644 --- a/max_height_stacking_cuboids.c +++ b/max_height_stacking_cuboids.c @@ -63,12 +63,13 @@ int main() { c2i[2][3] = {{38, 25, 45}, {76, 35, 3}}, c3i[6][3] = {{7, 11, 17}, {7, 17, 11}, {11, 7, 17}, {11, 17, 7}, {17, 7, 11}, {17, 11, 7}}; - struct two_d_arr c1, c2, c3; - two_d_arr_init(&c1, 3, 3, c1i); - two_d_arr_init(&c2, 2, 3, c2i); - two_d_arr_init(&c3, 6, 3, c3i); - printf("%d\n", maxHeight(c1.arr, c1.row_size, c1.col_size)); // expect: 190 - printf("%d\n", maxHeight(c2.arr, c2.row_size, c2.col_size)); // expect: 76 - printf("%d\n", maxHeight(c3.arr, c3.row_size, c3.col_size)); // expect: 102 - two_d_arr_free(&c1), two_d_arr_free(&c2), two_d_arr_free(&c3); + struct two_d_arr *c1 = two_d_arr_init(3, 3, c1i); + struct two_d_arr *c2 = two_d_arr_init(2, 3, c2i); + struct two_d_arr *c3 = two_d_arr_init(6, 3, c3i); + printf("%d\n", maxHeight(c1->arr, c1->row_size, c1->col_size)); // expect: 190 + printf("%d\n", maxHeight(c2->arr, c2->row_size, c2->col_size)); // expect: 76 + printf("%d\n", maxHeight(c3->arr, c3->row_size, c3->col_size)); // expect: 102 + two_d_arr_free(c1); + two_d_arr_free(c2); + two_d_arr_free(c3); } diff --git a/max_num_events_attended2.c b/max_num_events_attended2.c index ad9bfdb..f95030a 100644 --- a/max_num_events_attended2.c +++ b/max_num_events_attended2.c @@ -58,14 +58,17 @@ int main() { int e1i[3][3] = {{1, 2, 4}, {3, 4, 3}, {2, 3, 1}}; int e2i[3][3] = {{1, 2, 4}, {3, 4, 3}, {2, 3, 10}}; int e3i[4][3] = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}}; - struct two_d_arr e1, e2, e3; - two_d_arr_init(&e1, ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); - two_d_arr_init(&e2, ARRAY_SIZE(e2i), ARRAY_SIZE(e2i[0]), e2i); - two_d_arr_init(&e3, ARRAY_SIZE(e3i), ARRAY_SIZE(e3i[0]), e3i); - printf("%d\n", maxValue(e1.arr, e1.row_size, e1.col_size, 2)); // expect: 7 - printf("%d\n", maxValue(e2.arr, e2.row_size, e2.col_size, 2)); // expect: 10 - printf("%d\n", maxValue(e3.arr, e3.row_size, e3.col_size, 3)); // expect: 9 - two_d_arr_free(&e1); - two_d_arr_free(&e2); - two_d_arr_free(&e3); + struct two_d_arr *e1 = + two_d_arr_init(ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); + struct two_d_arr *e2 = + two_d_arr_init(ARRAY_SIZE(e2i), ARRAY_SIZE(e2i[0]), e2i); + struct two_d_arr *e3 = + two_d_arr_init(ARRAY_SIZE(e3i), ARRAY_SIZE(e3i[0]), e3i); + printf("%d\n", maxValue(e1->arr, e1->row_size, e1->col_size, 2)); // expect: 7 + printf("%d\n", + maxValue(e2->arr, e2->row_size, e2->col_size, 2)); // expect: 10 + printf("%d\n", maxValue(e3->arr, e3->row_size, e3->col_size, 3)); // expect: 9 + two_d_arr_free(e1); + two_d_arr_free(e2); + two_d_arr_free(e3); } diff --git a/max_num_fish_grid.c b/max_num_fish_grid.c index 1983937..41e60b4 100644 --- a/max_num_fish_grid.c +++ b/max_num_fish_grid.c @@ -39,11 +39,12 @@ int findMaxFish(int **grid, int gridSize, int *gridColSize) { int main() { int g1i[4][4] = {{0, 2, 1, 0}, {4, 0, 0, 3}, {1, 0, 0, 4}, {0, 3, 2, 0}}; int g2i[4][4] = {{1, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 1}}; - struct two_d_arr g1, g2; - two_d_arr_init(&g1, ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); - two_d_arr_init(&g2, ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); - printf("%d\n", findMaxFish(g1.arr, g1.row_size, g1.col_size)); // expect: 7 - printf("%d\n", findMaxFish(g2.arr, g2.row_size, g2.col_size)); // expect: 1 - two_d_arr_free(&g1); - two_d_arr_free(&g2); + struct two_d_arr *g1 = + two_d_arr_init(ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); + struct two_d_arr *g2 = + two_d_arr_init(ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); + printf("%d\n", findMaxFish(g1->arr, g1->row_size, g1->col_size)); // expect: 7 + printf("%d\n", findMaxFish(g2->arr, g2->row_size, g2->col_size)); // expect: 1 + two_d_arr_free(g1); + two_d_arr_free(g2); } diff --git a/max_num_flips_binary_matrix.c b/max_num_flips_binary_matrix.c index a5a5565..694835b 100644 --- a/max_num_flips_binary_matrix.c +++ b/max_num_flips_binary_matrix.c @@ -72,14 +72,13 @@ int minFlips(int **mat, int matSize, int *matColSize) { int main() { int m1i[2][2] = {{0, 0}, {0, 1}}, m2i[1][1] = {{0}}, m3i[2][3] = {{1, 0, 0}, {1, 0, 0}}; - struct two_d_arr m1, m2, m3; - two_d_arr_init(&m1, 2, 2, m1i); - two_d_arr_init(&m2, 1, 1, m2i); - two_d_arr_init(&m3, 2, 3, m3i); - printf("%d\n", minFlips(m1.arr, m1.row_size, m1.col_size)); // expect: 3 - printf("%d\n", minFlips(m2.arr, m2.row_size, m2.col_size)); // expect: 0 - printf("%d\n", minFlips(m3.arr, m3.row_size, m3.col_size)); // expect: -1 - two_d_arr_free(&m1); - two_d_arr_free(&m2); - two_d_arr_free(&m3); + struct two_d_arr *m1 = two_d_arr_init(2, 2, m1i); + struct two_d_arr *m2 = two_d_arr_init(1, 1, m2i); + struct two_d_arr *m3 = two_d_arr_init(2, 3, m3i); + printf("%d\n", minFlips(m1->arr, m1->row_size, m1->col_size)); // expect: 3 + printf("%d\n", minFlips(m2->arr, m2->row_size, m2->col_size)); // expect: 0 + printf("%d\n", minFlips(m3->arr, m3->row_size, m3->col_size)); // expect: -1 + two_d_arr_free(m1); + two_d_arr_free(m2); + two_d_arr_free(m3); } diff --git a/max_num_k_div_comp.c b/max_num_k_div_comp.c index c57b983..7139709 100644 --- a/max_num_k_div_comp.c +++ b/max_num_k_div_comp.c @@ -61,13 +61,12 @@ int main() { int e1i[4][2] = {{0, 2}, {1, 2}, {1, 3}, {2, 4}}, v1[] = {1, 8, 1, 4, 4}; int e2i[6][2] = {{0, 1}, {0, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}}, v2[] = {3, 0, 6, 1, 5, 2, 1}; - struct two_d_arr e1, e2; - two_d_arr_init(&e1, 4, 2, e1i); - two_d_arr_init(&e2, 6, 2, e2i); - printf("%d\n", maxKDivisibleComponents(5, e1.arr, e1.row_size, e1.col_size, + struct two_d_arr *e1 = two_d_arr_init(4, 2, e1i); + struct two_d_arr *e2 = two_d_arr_init(6, 2, e2i); + printf("%d\n", maxKDivisibleComponents(5, e1->arr, e1->row_size, e1->col_size, v1, ARRAY_SIZE(v1), 6)); // expect: 2 - printf("%d\n", maxKDivisibleComponents(7, e2.arr, e2.row_size, e2.col_size, + printf("%d\n", maxKDivisibleComponents(7, e2->arr, e2->row_size, e2->col_size, v2, ARRAY_SIZE(v2), 3)); // expect: 2 - two_d_arr_free(&e1); - two_d_arr_free(&e2); + two_d_arr_free(e1); + two_d_arr_free(e2); } diff --git a/max_num_moves_grid.c b/max_num_moves_grid.c index 617135c..119371f 100644 --- a/max_num_moves_grid.c +++ b/max_num_moves_grid.c @@ -35,11 +35,10 @@ int maxMoves(int **grid, int grid_size, int *grid_col_size) { int main() { int g1i[4][4] = {{2, 4, 3, 5}, {5, 4, 9, 3}, {3, 4, 2, 11}, {10, 9, 13, 15}}; int g2i[3][3] = {{3, 2, 4}, {2, 1, 9}, {1, 1, 7}}; - struct two_d_arr g1, g2; - two_d_arr_init(&g1, 4, 4, g1i); - two_d_arr_init(&g2, 3, 3, g2i); - printf("%d\n", maxMoves(g1.arr, g1.row_size, g1.col_size)); // expect: 3 - printf("%d\n", maxMoves(g2.arr, g2.row_size, g2.col_size)); // expect: 0 - two_d_arr_free(&g1); - two_d_arr_free(&g2); + struct two_d_arr *g1 = two_d_arr_init(4, 4, g1i); + struct two_d_arr *g2 = two_d_arr_init(3, 3, g2i); + printf("%d\n", maxMoves(g1->arr, g1->row_size, g1->col_size)); // expect: 3 + printf("%d\n", maxMoves(g2->arr, g2->row_size, g2->col_size)); // expect: 0 + two_d_arr_free(g1); + two_d_arr_free(g2); } diff --git a/max_num_point_grid_query.c b/max_num_point_grid_query.c index 1fbe18a..dc7bb56 100644 --- a/max_num_point_grid_query.c +++ b/max_num_point_grid_query.c @@ -102,13 +102,14 @@ int *maxPoints(int **grid, int gridSize, int *gridColSize, int *queries, int main() { int g1i[3][3] = {{1, 2, 3}, {2, 5, 7}, {3, 5, 1}}, q1[] = {5, 6, 2}, rs1; int g2i[2][3] = {{5, 2, 1}, {1, 1, 2}}, q2[] = {3}, rs2; - struct two_d_arr g1, g2; - two_d_arr_init(&g1, ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); - two_d_arr_init(&g2, ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); + struct two_d_arr *g1 = + two_d_arr_init(ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); + struct two_d_arr *g2 = + two_d_arr_init(ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); int *mp1 = - maxPoints(g1.arr, g1.row_size, g1.col_size, q1, ARRAY_SIZE(q1), &rs1); + maxPoints(g1->arr, g1->row_size, g1->col_size, q1, ARRAY_SIZE(q1), &rs1); int *mp2 = - maxPoints(g2.arr, g2.row_size, g2.col_size, q2, ARRAY_SIZE(q2), &rs2); + maxPoints(g2->arr, g2->row_size, g2->col_size, q2, ARRAY_SIZE(q2), &rs2); for (int i = 0; i < rs1; i++) printf("%d ", mp1[i]); // expect: 5 8 1 printf("\n"); @@ -117,6 +118,6 @@ int main() { printf("\n"); free(mp1); free(mp2); - two_d_arr_free(&g1); - two_d_arr_free(&g2); + two_d_arr_free(g1); + two_d_arr_free(g2); } diff --git a/max_path_quality_graph.c b/max_path_quality_graph.c index 9d3fc52..4700a46 100644 --- a/max_path_quality_graph.c +++ b/max_path_quality_graph.c @@ -75,17 +75,19 @@ int main() { int v2[] = {5, 10, 15, 20}, e2i[3][3] = {{0, 1, 10}, {1, 2, 10}, {0, 3, 10}}; int v3[] = {1, 2, 3, 4}, e3i[4][3] = {{0, 1, 10}, {1, 2, 11}, {2, 3, 12}, {1, 3, 13}}; - struct two_d_arr e1, e2, e3; - two_d_arr_init(&e1, ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); - two_d_arr_init(&e2, ARRAY_SIZE(e2i), ARRAY_SIZE(e2i[0]), e2i); - two_d_arr_init(&e3, ARRAY_SIZE(e3i), ARRAY_SIZE(e3i[0]), e3i); - printf("%d\n", maximalPathQuality(v1, ARRAY_SIZE(v1), e1.arr, e1.row_size, - e1.col_size, 49)); // expect: 75 - printf("%d\n", maximalPathQuality(v1, ARRAY_SIZE(v1), e1.arr, e1.row_size, - e1.col_size, 30)); // expect: 25 - printf("%d\n", maximalPathQuality(v1, ARRAY_SIZE(v1), e1.arr, e1.row_size, - e1.col_size, 50)); // expect: 50 - two_d_arr_free(&e1); - two_d_arr_free(&e2); - two_d_arr_free(&e3); + struct two_d_arr *e1 = + two_d_arr_init(ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); + struct two_d_arr *e2 = + two_d_arr_init(ARRAY_SIZE(e2i), ARRAY_SIZE(e2i[0]), e2i); + struct two_d_arr *e3 = + two_d_arr_init(ARRAY_SIZE(e3i), ARRAY_SIZE(e3i[0]), e3i); + printf("%d\n", maximalPathQuality(v1, ARRAY_SIZE(v1), e1->arr, e1->row_size, + e1->col_size, 49)); // expect: 75 + printf("%d\n", maximalPathQuality(v1, ARRAY_SIZE(v1), e1->arr, e1->row_size, + e1->col_size, 30)); // expect: 25 + printf("%d\n", maximalPathQuality(v1, ARRAY_SIZE(v1), e1->arr, e1->row_size, + e1->col_size, 50)); // expect: 50 + two_d_arr_free(e1); + two_d_arr_free(e2); + two_d_arr_free(e3); } diff --git a/max_side_length_square.c b/max_side_length_square.c index 5c6e701..2ae3fd7 100644 --- a/max_side_length_square.c +++ b/max_side_length_square.c @@ -41,13 +41,12 @@ int main() { {1, 1, 3, 2, 4, 3, 2}, {1, 1, 3, 2, 4, 3, 2}, {1, 1, 3, 2, 4, 3, 2}}; int m2i[4][5] = { {2, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 2, 2, 2, 2}}; - struct two_d_arr m1, m2; - two_d_arr_init(&m1, 3, 7, m1i); - two_d_arr_init(&m2, 4, 5, m2i); + struct two_d_arr *m1 = two_d_arr_init(3, 7, m1i); + struct two_d_arr *m2 = two_d_arr_init(4, 5, m2i); printf("%d\n", - maxSideLength(m1.arr, m1.row_size, m1.col_size, 4)); // expect: 2 + maxSideLength(m1->arr, m1->row_size, m1->col_size, 4)); // expect: 2 printf("%d\n", - maxSideLength(m2.arr, m2.row_size, m2.col_size, 1)); // expect: 0 - two_d_arr_free(&m1); - two_d_arr_free(&m2); + maxSideLength(m2->arr, m2->row_size, m2->col_size, 1)); // expect: 0 + two_d_arr_free(m1); + two_d_arr_free(m2); } diff --git a/max_subarr_remove_conflict_pair.c b/max_subarr_remove_conflict_pair.c index a225f61..8e0d9b3 100644 --- a/max_subarr_remove_conflict_pair.c +++ b/max_subarr_remove_conflict_pair.c @@ -50,11 +50,14 @@ long long maxSubarrays(int n, int **conflictingPairs, int conflictingPairsSize, int main() { int cp1i[2][2] = {{2, 3}, {1, 4}}, cp2i[3][2] = {{1, 2}, {2, 5}, {3, 5}}; - struct two_d_arr cp1, cp2; - two_d_arr_init(&cp1, ARRAY_SIZE(cp1i), ARRAY_SIZE(cp1i[0]), cp1i); - two_d_arr_init(&cp2, ARRAY_SIZE(cp2i), ARRAY_SIZE(cp2i[0]), cp2i); + struct two_d_arr *cp1 = + two_d_arr_init(ARRAY_SIZE(cp1i), ARRAY_SIZE(cp1i[0]), cp1i); + struct two_d_arr *cp2 = + two_d_arr_init(ARRAY_SIZE(cp2i), ARRAY_SIZE(cp2i[0]), cp2i); printf("%lld\n", - maxSubarrays(4, cp1.arr, cp1.row_size, cp1.col_size)); // expect: 9 + maxSubarrays(4, cp1->arr, cp1->row_size, cp1->col_size)); // expect: 9 printf("%lld\n", - maxSubarrays(5, cp2.arr, cp2.row_size, cp2.col_size)); // expect: 12 + maxSubarrays(5, cp2->arr, cp2->row_size, cp2->col_size)); // expect: 12 + two_d_arr_free(cp1); + two_d_arr_free(cp2); } diff --git a/maximise_num_target_node_tree2.c b/maximise_num_target_node_tree2.c index f00d00e..59606a9 100644 --- a/maximise_num_target_node_tree2.c +++ b/maximise_num_target_node_tree2.c @@ -115,15 +115,18 @@ int main() { rs1; int e12i[4][2] = {{0, 1}, {0, 2}, {0, 3}, {0, 4}}, e22i[3][2] = {{0, 1}, {1, 2}, {2, 3}}, rs2; - struct two_d_arr e11, e21, e12, e22; - two_d_arr_init(&e11, ARRAY_SIZE(e11i), ARRAY_SIZE(e11i[0]), e11i); - two_d_arr_init(&e21, ARRAY_SIZE(e21i), ARRAY_SIZE(e21i[0]), e21i); - two_d_arr_init(&e12, ARRAY_SIZE(e12i), ARRAY_SIZE(e12i[0]), e12i); - two_d_arr_init(&e22, ARRAY_SIZE(e22i), ARRAY_SIZE(e22i[0]), e22i); - int *mtn1 = maxTargetNodes(e11.arr, e11.row_size, e11.col_size, e21.arr, - e21.row_size, e21.col_size, &rs1); - int *mtn2 = maxTargetNodes(e12.arr, e12.row_size, e12.col_size, e22.arr, - e22.row_size, e22.col_size, &rs2); + struct two_d_arr *e11 = + two_d_arr_init(ARRAY_SIZE(e11i), ARRAY_SIZE(e11i[0]), e11i); + struct two_d_arr *e21 = + two_d_arr_init(ARRAY_SIZE(e21i), ARRAY_SIZE(e21i[0]), e21i); + struct two_d_arr *e12 = + two_d_arr_init(ARRAY_SIZE(e12i), ARRAY_SIZE(e12i[0]), e12i); + struct two_d_arr *e22 = + two_d_arr_init(ARRAY_SIZE(e22i), ARRAY_SIZE(e22i[0]), e22i); + int *mtn1 = maxTargetNodes(e11->arr, e11->row_size, e11->col_size, e21->arr, + e21->row_size, e21->col_size, &rs1); + int *mtn2 = maxTargetNodes(e12->arr, e12->row_size, e12->col_size, e22->arr, + e22->row_size, e22->col_size, &rs2); for (int i = 0; i < rs1; i++) printf("%d ", mtn1[i]); // expect: 8 7 7 8 8 printf("\n"); @@ -132,8 +135,8 @@ int main() { printf("\n"); free(mtn1); free(mtn2); - two_d_arr_free(&e11); - two_d_arr_free(&e21); - two_d_arr_free(&e12); - two_d_arr_free(&e22); + two_d_arr_free(e11); + two_d_arr_free(e21); + two_d_arr_free(e12); + two_d_arr_free(e22); } diff --git a/maximum_matrix_sum.c b/maximum_matrix_sum.c index c0d3604..f917419 100644 --- a/maximum_matrix_sum.c +++ b/maximum_matrix_sum.c @@ -29,12 +29,12 @@ long long maxMatrixSum(int **matrix, int matrixSize, int *matrixColSize) { int main() { int m1i[2][2] = {{1, -1}, {-1, 1}}, m2i[3][3] = {{1, 2, 3}, {-1, -2, -3}, {1, 2, 3}}; - struct two_d_arr m1, m2; - two_d_arr_init(&m1, 2, 2, m1i); - two_d_arr_init(&m2, 3, 3, m2i); - printf("%lld\n", maxMatrixSum(m1.arr, m1.row_size, m1.col_size)); // expect: 4 - printf("%lld\n", maxMatrixSum(m2.arr, m2.row_size, m2.col_size)); // expect: - // 16 - two_d_arr_free(&m1); - two_d_arr_free(&m2); + struct two_d_arr *m1 = two_d_arr_init(2, 2, m1i); + struct two_d_arr *m2 = two_d_arr_init(3, 3, m2i); + printf("%lld\n", + maxMatrixSum(m1->arr, m1->row_size, m1->col_size)); // expect: 4 + printf("%lld\n", + maxMatrixSum(m2->arr, m2->row_size, m2->col_size)); // expect: 16 + two_d_arr_free(m1); + two_d_arr_free(m2); } diff --git a/merge_intervals.c b/merge_intervals.c index d38a467..0633b26 100644 --- a/merge_intervals.c +++ b/merge_intervals.c @@ -34,12 +34,13 @@ int **merge(int **intervals, int intervalsSize, int *intervalsColSize, int main() { int i1i[4][2] = {{1, 3}, {2, 6}, {8, 10}, {15, 18}}, i2i[2][2] = {{1, 4}, {4, 5}}; - struct two_d_arr i1, i2; - two_d_arr_init(&i1, ARRAY_SIZE(i1i), ARRAY_SIZE(i1i[0]), i1i); - two_d_arr_init(&i2, ARRAY_SIZE(i2i), ARRAY_SIZE(i2i[0]), i2i); + struct two_d_arr *i1 = + two_d_arr_init(ARRAY_SIZE(i1i), ARRAY_SIZE(i1i[0]), i1i); + struct two_d_arr *i2 = + two_d_arr_init(ARRAY_SIZE(i2i), ARRAY_SIZE(i2i[0]), i2i); int rs1, *rcs1, rs2, *rcs2; - int **m1 = merge(i1.arr, i1.row_size, i1.col_size, &rs1, &rcs1); - int **m2 = merge(i2.arr, i2.row_size, i2.col_size, &rs2, &rcs2); + int **m1 = merge(i1->arr, i1->row_size, i1->col_size, &rs1, &rcs1); + int **m2 = merge(i2->arr, i2->row_size, i2->col_size, &rs2, &rcs2); for (int i = 0; i < rs1; i++) { for (int j = 0; j < rcs1[i]; j++) printf("%d ", m1[i][j]); // expect: [[1,6],[8,10],[15,18]] @@ -55,9 +56,9 @@ int main() { for (int i = 0; i < rs1; i++) free(m1[i]); free(m1); - two_d_arr_free(&i1); + two_d_arr_free(i1); for (int i = 0; i < rs2; i++) free(m2[i]); free(m2); - two_d_arr_free(&i2); + two_d_arr_free(i2); } diff --git a/merge_two_2d_arr_sum.c b/merge_two_2d_arr_sum.c index 5263902..f4696b8 100644 --- a/merge_two_2d_arr_sum.c +++ b/merge_two_2d_arr_sum.c @@ -53,15 +53,18 @@ int main() { 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); + struct two_d_arr *n11 = + two_d_arr_init(ARRAY_SIZE(n11i), ARRAY_SIZE(n11i[0]), n11i); + struct two_d_arr *n21 = + two_d_arr_init(ARRAY_SIZE(n21i), ARRAY_SIZE(n21i[0]), n21i); + struct two_d_arr *n12 = + two_d_arr_init(ARRAY_SIZE(n12i), ARRAY_SIZE(n12i[0]), n12i); + struct two_d_arr *n22 = + two_d_arr_init(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]] @@ -82,8 +85,8 @@ int main() { 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); + two_d_arr_free(n11); + two_d_arr_free(n21); + two_d_arr_free(n12); + two_d_arr_free(n22); } diff --git a/min_cost_valid_path.c b/min_cost_valid_path.c index b11562a..b14eff0 100644 --- a/min_cost_valid_path.c +++ b/min_cost_valid_path.c @@ -98,14 +98,16 @@ int main() { int g1i[4][4] = {{1, 1, 1, 1}, {2, 2, 2, 2}, {1, 1, 1, 1}, {2, 2, 2, 2}}; int g2i[3][3] = {{1, 1, 3}, {3, 2, 2}, {1, 1, 4}}; int g3i[2][2] = {{1, 2}, {4, 3}}; - struct two_d_arr g1, g2, g3; - two_d_arr_init(&g1, ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); - two_d_arr_init(&g2, ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); - two_d_arr_init(&g3, ARRAY_SIZE(g3i), ARRAY_SIZE(g3i[0]), g3i); - printf("%d\n", minCost(g1.arr, g1.row_size, g1.col_size)); // expect: 3 - printf("%d\n", minCost(g2.arr, g2.row_size, g2.col_size)); // expect: 0 - printf("%d\n", minCost(g3.arr, g3.row_size, g3.col_size)); // expect: 1 - two_d_arr_free(&g1); - two_d_arr_free(&g2); - two_d_arr_free(&g3); + struct two_d_arr *g1 = + two_d_arr_init(ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); + struct two_d_arr *g2 = + two_d_arr_init(ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); + struct two_d_arr *g3 = + two_d_arr_init(ARRAY_SIZE(g3i), ARRAY_SIZE(g3i[0]), g3i); + printf("%d\n", minCost(g1->arr, g1->row_size, g1->col_size)); // expect: 3 + printf("%d\n", minCost(g2->arr, g2->row_size, g2->col_size)); // expect: 0 + printf("%d\n", minCost(g3->arr, g3->row_size, g3->col_size)); // expect: 1 + two_d_arr_free(g1); + two_d_arr_free(g2); + two_d_arr_free(g3); } diff --git a/min_num_flip_bin_grid_palin2.c b/min_num_flip_bin_grid_palin2.c index fe3ffdf..162d526 100644 --- a/min_num_flip_bin_grid_palin2.c +++ b/min_num_flip_bin_grid_palin2.c @@ -37,14 +37,16 @@ int minFlips(int **grid, int gridSize, int *gridColSize) { int main() { int g1i[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, g2i[3][2] = {{0, 1}, {0, 1}, {0, 0}}, g3i[2][1] = {{1}, {1}}; - struct two_d_arr g1, g2, g3; - two_d_arr_init(&g1, ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); - two_d_arr_init(&g2, ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); - two_d_arr_init(&g3, ARRAY_SIZE(g3i), ARRAY_SIZE(g3i[0]), g3i); - printf("%d\n", minFlips(g1.arr, g1.row_size, g1.col_size)); // expect: 3 - printf("%d\n", minFlips(g2.arr, g2.row_size, g2.col_size)); // expect: 2 - printf("%d\n", minFlips(g3.arr, g3.row_size, g3.col_size)); // expect: 2 - two_d_arr_free(&g1); - two_d_arr_free(&g2); - two_d_arr_free(&g3); + struct two_d_arr *g1 = + two_d_arr_init(ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); + struct two_d_arr *g2 = + two_d_arr_init(ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); + struct two_d_arr *g3 = + two_d_arr_init(ARRAY_SIZE(g3i), ARRAY_SIZE(g3i[0]), g3i); + printf("%d\n", minFlips(g1->arr, g1->row_size, g1->col_size)); // expect: 3 + printf("%d\n", minFlips(g2->arr, g2->row_size, g2->col_size)); // expect: 2 + printf("%d\n", minFlips(g3->arr, g3->row_size, g3->col_size)); // expect: 2 + two_d_arr_free(g1); + two_d_arr_free(g2); + two_d_arr_free(g3); } diff --git a/min_obstacle_removal_corner.c b/min_obstacle_removal_corner.c index df38cd4..2f0c10b 100644 --- a/min_obstacle_removal_corner.c +++ b/min_obstacle_removal_corner.c @@ -117,12 +117,12 @@ int minimumObstacles(int **grid, int gridSize, int *gridColSize) { int main() { int g1i[3][3] = {{0, 1, 1}, {1, 1, 0}, {1, 1, 0}}, g2i[3][5] = {{0, 1, 0, 0, 0}, {0, 1, 0, 1, 0}, {0, 0, 0, 1, 0}}; - struct two_d_arr g1, g2; - two_d_arr_init(&g1, 3, 3, g1i); - two_d_arr_init(&g2, 3, 5, g2i); + struct two_d_arr *g1 = two_d_arr_init(3, 3, g1i); + struct two_d_arr *g2 = two_d_arr_init(3, 5, g2i); printf("%d\n", - minimumObstacles(g1.arr, g1.row_size, g1.col_size)); // expect: 2 + minimumObstacles(g1->arr, g1->row_size, g1->col_size)); // expect: 2 printf("%d\n", - minimumObstacles(g2.arr, g2.row_size, g2.col_size)); // expect: 0 - two_d_arr_free(&g1), two_d_arr_free(&g2); + minimumObstacles(g2->arr, g2->row_size, g2->col_size)); // expect: 0 + two_d_arr_free(g1); + two_d_arr_free(g2); } diff --git a/min_operations_write_y_grid.c b/min_operations_write_y_grid.c index 96f082e..c3f13e0 100644 --- a/min_operations_write_y_grid.c +++ b/min_operations_write_y_grid.c @@ -48,13 +48,12 @@ int main() { {2, 2, 2, 0, 1}, {2, 2, 2, 2, 2}, {2, 1, 2, 2, 2}}; - struct two_d_arr g1, g2; - two_d_arr_init(&g1, 3, 3, g1i); - two_d_arr_init(&g2, 5, 2, g2i); - printf("%d\n", minimumOperationsToWriteY(g1.arr, g1.row_size, - g1.col_size)); // expect: 3 - printf("%d\n", minimumOperationsToWriteY(g2.arr, g2.row_size, - g2.col_size)); // expect: 12 - two_d_arr_free(&g1); - two_d_arr_free(&g2); + struct two_d_arr *g1 = two_d_arr_init(3, 3, g1i); + struct two_d_arr *g2 = two_d_arr_init(5, 2, g2i); + printf("%d\n", minimumOperationsToWriteY(g1->arr, g1->row_size, + g1->col_size)); // expect: 3 + printf("%d\n", minimumOperationsToWriteY(g2->arr, g2->row_size, + g2->col_size)); // expect: 12 + two_d_arr_free(g1); + two_d_arr_free(g2); } diff --git a/min_ops_uni_val_grid.c b/min_ops_uni_val_grid.c index aa2f025..ff4964e 100644 --- a/min_ops_uni_val_grid.c +++ b/min_ops_uni_val_grid.c @@ -33,17 +33,19 @@ int minOperations(int **grid, int gridSize, int *gridColSize, int x) { int main() { int g1i[2][2] = {{2, 4}, {6, 8}}, g2i[2][2] = {{1, 5}, {2, 3}}, g3i[2][2] = {{1, 2}, {3, 4}}; - struct two_d_arr g1, g2, g3; - two_d_arr_init(&g1, ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); - two_d_arr_init(&g2, ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); - two_d_arr_init(&g3, ARRAY_SIZE(g3i), ARRAY_SIZE(g3i[0]), g3i); + struct two_d_arr *g1 = + two_d_arr_init(ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); + struct two_d_arr *g2 = + two_d_arr_init(ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); + struct two_d_arr *g3 = + two_d_arr_init(ARRAY_SIZE(g3i), ARRAY_SIZE(g3i[0]), g3i); printf("%d\n", - minOperations(g1.arr, g1.row_size, g1.col_size, 2)); // expect: 4 + minOperations(g1->arr, g1->row_size, g1->col_size, 2)); // expect: 4 printf("%d\n", - minOperations(g2.arr, g2.row_size, g2.col_size, 1)); // expect: 5 + minOperations(g2->arr, g2->row_size, g2->col_size, 1)); // expect: 5 printf("%d\n", - minOperations(g3.arr, g3.row_size, g3.col_size, 2)); // expect: -1 - two_d_arr_free(&g1); - two_d_arr_free(&g2); - two_d_arr_free(&g3); + minOperations(g3->arr, g3->row_size, g3->col_size, 2)); // expect: -1 + two_d_arr_free(g1); + two_d_arr_free(g2); + two_d_arr_free(g3); } diff --git a/min_total_distance_traveled.c b/min_total_distance_traveled.c index b41c3be..9350a98 100644 --- a/min_total_distance_traveled.c +++ b/min_total_distance_traveled.c @@ -232,14 +232,15 @@ long long minimumTotalDistance(int *robot, int robotSize, int **factory, int main() { int r1[] = {0, 4, 6}, f1i[2][2] = {{2, 2}, {6, 2}}; int r2[] = {1, -1}, f2i[2][2] = {{-2, 1}, {2, 1}}; - struct two_d_arr f1, f2; - two_d_arr_init(&f1, 2, 2, f1i); - two_d_arr_init(&f2, 2, 2, f2i); - printf("%lld\n", minimumTotalDistance(r1, ARRAY_SIZE(r1), f1.arr, f1.row_size, - f1.col_size)); // expect: 4 + struct two_d_arr *f1 = two_d_arr_init(2, 2, f1i); + struct two_d_arr *f2 = two_d_arr_init(2, 2, f2i); + printf("%lld\n", + minimumTotalDistance(r1, ARRAY_SIZE(r1), f1->arr, f1->row_size, + f1->col_size)); // expect: 4 - printf("%lld\n", minimumTotalDistance(r2, ARRAY_SIZE(r2), f2.arr, f2.row_size, - f2.col_size)); // expect: 2 - two_d_arr_free(&f1); - two_d_arr_free(&f2); + printf("%lld\n", + minimumTotalDistance(r2, ARRAY_SIZE(r2), f2->arr, f2->row_size, + f2->col_size)); // expect: 2 + two_d_arr_free(f1); + two_d_arr_free(f2); } diff --git a/most_beautiful_item_query.c b/most_beautiful_item_query.c index 42749fd..caae884 100644 --- a/most_beautiful_item_query.c +++ b/most_beautiful_item_query.c @@ -58,16 +58,15 @@ int main() { q1[] = {1, 2, 3, 4, 5, 6}, rs1; int i2i[4][2] = {{1, 2}, {1, 2}, {1, 3}, {1, 4}}, q2[] = {1}, rs2; int i3i[1][2] = {{10, 1000}}, q3[] = {5}, rs3; - struct two_d_arr i1, i2, i3; - two_d_arr_init(&i1, 5, 2, i1i); - two_d_arr_init(&i2, 4, 2, i2i); - two_d_arr_init(&i3, 1, 2, i3i); - int *mb1 = - maximumBeauty(i1.arr, i1.row_size, i1.col_size, q1, ARRAY_SIZE(q1), &rs1); - int *mb2 = - maximumBeauty(i2.arr, i2.row_size, i2.col_size, q2, ARRAY_SIZE(q2), &rs2); - int *mb3 = - maximumBeauty(i3.arr, i3.row_size, i3.col_size, q3, ARRAY_SIZE(q3), &rs3); + struct two_d_arr *i1 = two_d_arr_init(5, 2, i1i); + struct two_d_arr *i2 = two_d_arr_init(4, 2, i2i); + struct two_d_arr *i3 = two_d_arr_init(1, 2, i3i); + int *mb1 = maximumBeauty(i1->arr, i1->row_size, i1->col_size, q1, + ARRAY_SIZE(q1), &rs1); + int *mb2 = maximumBeauty(i2->arr, i2->row_size, i2->col_size, q2, + ARRAY_SIZE(q2), &rs2); + int *mb3 = maximumBeauty(i3->arr, i3->row_size, i3->col_size, q3, + ARRAY_SIZE(q3), &rs3); for (int i = 0; i < rs1; i++) printf("%d ", mb1[i]); // expect: 2 4 5 5 6 6 printf("\n"); @@ -78,5 +77,7 @@ int main() { printf("%d ", mb3[i]); // expect: 0 printf("\n"); free(mb1), free(mb2), free(mb3); - two_d_arr_free(&i1), two_d_arr_free(&i2), two_d_arr_free(&i3); + two_d_arr_free(i1); + two_d_arr_free(i2); + two_d_arr_free(i3); } diff --git a/most_profitable_path_tree.c b/most_profitable_path_tree.c index 458a21d..b433e18 100644 --- a/most_profitable_path_tree.c +++ b/most_profitable_path_tree.c @@ -101,13 +101,14 @@ int mostProfitablePath(int **edges, int edgesSize, int *edgesColSize, int bob, int main() { int e1i[4][2] = {{0, 1}, {1, 2}, {1, 3}, {3, 4}}, a1[] = {-2, 4, 2, -4, 6}; int e2i[1][2] = {{0, 1}}, a2[] = {-7280, 2350}; - struct two_d_arr e1, e2; - two_d_arr_init(&e1, ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); - two_d_arr_init(&e2, ARRAY_SIZE(e2i), ARRAY_SIZE(e2i[0]), e2i); - printf("%d\n", mostProfitablePath(e1.arr, e1.row_size, e1.col_size, 3, a1, + struct two_d_arr *e1 = + two_d_arr_init(ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); + struct two_d_arr *e2 = + two_d_arr_init(ARRAY_SIZE(e2i), ARRAY_SIZE(e2i[0]), e2i); + printf("%d\n", mostProfitablePath(e1->arr, e1->row_size, e1->col_size, 3, a1, ARRAY_SIZE(a1))); // expect: 6 - printf("%d\n", mostProfitablePath(e2.arr, e2.row_size, e2.col_size, 1, a2, + printf("%d\n", mostProfitablePath(e2->arr, e2->row_size, e2->col_size, 1, a2, ARRAY_SIZE(a2))); // expect: -7280 - two_d_arr_free(&e1); - two_d_arr_free(&e2); + two_d_arr_free(e1); + two_d_arr_free(e2); } diff --git a/num_equivalent_domino_pair.c b/num_equivalent_domino_pair.c index eb9e3d6..e7d384d 100644 --- a/num_equivalent_domino_pair.c +++ b/num_equivalent_domino_pair.c @@ -23,13 +23,14 @@ int numEquivDominoPairs(int **dominoes, int dominoesSize, int main() { int d1i[4][2] = {{1, 2}, {2, 1}, {3, 4}, {5, 6}}; int d2i[5][2] = {{1, 2}, {1, 2}, {1, 1}, {1, 2}, {2, 2}}; - struct two_d_arr d1, d2; - two_d_arr_init(&d1, ARRAY_SIZE(d1i), ARRAY_SIZE(d1i[0]), d1i); - two_d_arr_init(&d2, ARRAY_SIZE(d2i), ARRAY_SIZE(d2i[0]), d2i); + struct two_d_arr *d1 = + two_d_arr_init(ARRAY_SIZE(d1i), ARRAY_SIZE(d1i[0]), d1i); + struct two_d_arr *d2 = + two_d_arr_init(ARRAY_SIZE(d2i), ARRAY_SIZE(d2i[0]), d2i); printf("%d\n", - numEquivDominoPairs(d1.arr, d1.row_size, d1.col_size)); // expect: 1 + numEquivDominoPairs(d1->arr, d1->row_size, d1->col_size)); // expect: 1 printf("%d\n", - numEquivDominoPairs(d2.arr, d2.row_size, d2.col_size)); // expect: 3 - two_d_arr_free(&d1); - two_d_arr_free(&d2); + numEquivDominoPairs(d2->arr, d2->row_size, d2->col_size)); // expect: 3 + two_d_arr_free(d1); + two_d_arr_free(d2); } diff --git a/range_product_queries_powers.c b/range_product_queries_powers.c index fb70f41..1bb45d1 100644 --- a/range_product_queries_powers.c +++ b/range_product_queries_powers.c @@ -61,11 +61,12 @@ int *productQueries(int n, int **queries, int queriesSize, int *queriesColSize, int main() { int q1i[3][2] = {{0, 1}, {2, 2}, {0, 3}}, q2i[1][2] = {{0, 0}}; - struct two_d_arr q1, q2; - two_d_arr_init(&q1, ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); - two_d_arr_init(&q2, ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); - int rs1, *pq1 = productQueries(15, q1.arr, q1.row_size, q1.col_size, &rs1); - int rs2, *pq2 = productQueries(2, q2.arr, q2.row_size, q2.col_size, &rs2); + struct two_d_arr *q1 = + two_d_arr_init(ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); + struct two_d_arr *q2 = + two_d_arr_init(ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); + int rs1, *pq1 = productQueries(15, q1->arr, q1->row_size, q1->col_size, &rs1); + int rs2, *pq2 = productQueries(2, q2->arr, q2->row_size, q2->col_size, &rs2); for (int i = 0; i < rs1; i++) printf("%d ", pq1[i]); // expect: 2 4 6 printf("\n"); @@ -74,6 +75,6 @@ int main() { printf("\n"); free(pq1); free(pq2); - two_d_arr_free(&q1); - two_d_arr_free(&q2); + two_d_arr_free(q1); + two_d_arr_free(q2); } diff --git a/range_sum_query_2d.c b/range_sum_query_2d.c index 833f104..c0ce702 100644 --- a/range_sum_query_2d.c +++ b/range_sum_query_2d.c @@ -45,13 +45,12 @@ int main() { {1, 2, 0, 1, 5}, {4, 1, 0, 1, 7}, {1, 0, 3, 0, 5}}; - struct two_d_arr matrix; - two_d_arr_init(&matrix, 5, 5, matrix_input); + struct two_d_arr *matrix = two_d_arr_init(5, 5, matrix_input); NumMatrix *obj = - numMatrixCreate(matrix.arr, matrix.row_size, matrix.col_size); + numMatrixCreate(matrix->arr, matrix->row_size, matrix->col_size); printf("%d\n", numMatrixSumRegion(obj, 2, 1, 4, 3)); // expect: 8 printf("%d\n", numMatrixSumRegion(obj, 1, 1, 2, 2)); // expect: 11 printf("%d\n", numMatrixSumRegion(obj, 1, 2, 2, 4)); // expect: 12 numMatrixFree(obj); - two_d_arr_free(&matrix); + two_d_arr_free(matrix); } @@ -54,9 +54,8 @@ int func(int **edges, int edgesSize, int *edgesColSize); int main() { // edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]] int edges_input[6][2] = {{0, 1}, {0, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}}; - struct two_d_arr edges; - two_d_arr_init(&edges, ARRAY_SIZE(edges_input), ARRAY_SIZE(edges_input[0]), edges_input); + struct two_d_arr *edges = two_d_arr_init(ARRAY_SIZE(edges_input), ARRAY_SIZE(edges_input[0]), edges_input); int res = func(edges.arr, edges.row_size, edges.col_size); - two_d_arr_free(&edges); + two_d_arr_free(edges); } ``` diff --git a/redundant_connection.c b/redundant_connection.c index 43036d1..4874f8b 100644 --- a/redundant_connection.c +++ b/redundant_connection.c @@ -56,17 +56,20 @@ int *findRedundantConnection(int **edges, int edgesSize, int *edgesColSize, int main() { int e1i[3][2] = {{1, 2}, {1, 3}, {2, 3}}, rs1; int e2i[5][2] = {{1, 2}, {2, 3}, {3, 4}, {1, 4}, {1, 5}}, rs2; - struct two_d_arr e1, e2; - two_d_arr_init(&e1, ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); - two_d_arr_init(&e2, ARRAY_SIZE(e2i), ARRAY_SIZE(e2i[0]), e2i); - int *frc1 = findRedundantConnection(e1.arr, e1.row_size, e1.col_size, &rs1); - int *frc2 = findRedundantConnection(e2.arr, e2.row_size, e2.col_size, &rs2); + struct two_d_arr *e1 = + two_d_arr_init(ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); + struct two_d_arr *e2 = + two_d_arr_init(ARRAY_SIZE(e2i), ARRAY_SIZE(e2i[0]), e2i); + int *frc1 = + findRedundantConnection(e1->arr, e1->row_size, e1->col_size, &rs1); + int *frc2 = + findRedundantConnection(e2->arr, e2->row_size, e2->col_size, &rs2); for (int i = 0; i < rs1; i++) printf("%d ", frc1[i]); // expect: 2 3 printf("\n"); for (int i = 0; i < rs2; i++) printf("%d ", frc2[i]); // expect: 1 4 printf("\n"); - free(frc1), two_d_arr_free(&e1); - free(frc2), two_d_arr_free(&e2); + free(frc1), two_d_arr_free(e1); + free(frc2), two_d_arr_free(e2); } diff --git a/rotting_oranges.c b/rotting_oranges.c index f59eff9..83d6bb3 100644 --- a/rotting_oranges.c +++ b/rotting_oranges.c @@ -72,15 +72,16 @@ int orangesRotting(int **grid, int gridSize, int *gridColSize) { int main() { int g1i[3][3] = {{2, 1, 1}, {1, 1, 0}, {0, 1, 1}}, g2i[3][3] = {{2, 1, 1}, {1, 1, 0}, {0, 1, 1}}, g3i[1][2] = {{0, 2}}; - struct two_d_arr g1, g2, g3; - two_d_arr_init(&g1, 3, 3, g1i); - two_d_arr_init(&g2, 3, 3, g2i); - two_d_arr_init(&g3, 1, 2, g3i); - printf("%d\n", orangesRotting(g1.arr, g1.row_size, g1.col_size)); // expect: 4 - printf("%d\n", orangesRotting(g2.arr, g2.row_size, g2.col_size)); // expect: - // -1 - printf("%d\n", orangesRotting(g3.arr, g3.row_size, g3.col_size)); // expect: 0 - two_d_arr_free(&g1); - two_d_arr_free(&g2); - two_d_arr_free(&g3); + struct two_d_arr *g1 = two_d_arr_init(3, 3, g1i); + struct two_d_arr *g2 = two_d_arr_init(3, 3, g2i); + struct two_d_arr *g3 = two_d_arr_init(1, 2, g3i); + printf("%d\n", + orangesRotting(g1->arr, g1->row_size, g1->col_size)); // expect: 4 + printf("%d\n", + orangesRotting(g2->arr, g2->row_size, g2->col_size)); // expect: -1 + printf("%d\n", + orangesRotting(g3->arr, g3->row_size, g3->col_size)); // expect: 0 + two_d_arr_free(g1); + two_d_arr_free(g2); + two_d_arr_free(g3); } diff --git a/russian_doll_envelopes.c b/russian_doll_envelopes.c index 0849180..e29f2d7 100644 --- a/russian_doll_envelopes.c +++ b/russian_doll_envelopes.c @@ -50,11 +50,14 @@ int maxEnvelopes(int **envelopes, int envelopesSize, int *envelopesColSize) { int main() { int e1i[4][2] = {{5, 4}, {6, 4}, {6, 7}, {2, 3}}, e2i[3][2] = {{1, 1}, {1, 1}, {1, 1}}; - struct two_d_arr e1, e2; - two_d_arr_init(&e1, ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); - two_d_arr_init(&e2, ARRAY_SIZE(e2i), ARRAY_SIZE(e2i[0]), e2i); - printf("%d\n", maxEnvelopes(e1.arr, e1.row_size, e1.col_size)); // expect: 3 - printf("%d\n", maxEnvelopes(e2.arr, e2.row_size, e2.col_size)); // expect: 1 - two_d_arr_free(&e1); - two_d_arr_free(&e2); + struct two_d_arr *e1 = + two_d_arr_init(ARRAY_SIZE(e1i), ARRAY_SIZE(e1i[0]), e1i); + struct two_d_arr *e2 = + two_d_arr_init(ARRAY_SIZE(e2i), ARRAY_SIZE(e2i[0]), e2i); + printf("%d\n", + maxEnvelopes(e1->arr, e1->row_size, e1->col_size)); // expect: 3 + printf("%d\n", + maxEnvelopes(e2->arr, e2->row_size, e2->col_size)); // expect: 1 + two_d_arr_free(e1); + two_d_arr_free(e2); } diff --git a/set_matrix_zeroes.c b/set_matrix_zeroes.c index be0e332..6bb14e6 100644 --- a/set_matrix_zeroes.c +++ b/set_matrix_zeroes.c @@ -24,21 +24,22 @@ void setZeroes(int **matrix, int matrixSize, int *matrixColSize) { int main() { int m1i[3][3] = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}, m2i[3][4] = {{0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}}; - struct two_d_arr m1, m2; - two_d_arr_init(&m1, ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i); - two_d_arr_init(&m2, ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i); - setZeroes(m1.arr, m1.row_size, m1.col_size); - setZeroes(m2.arr, m2.row_size, m2.col_size); - for (int i = 0; i < m1.row_size; i++) { - for (int j = 0; j < m1.col_size[i]; j++) + struct two_d_arr *m1 = + two_d_arr_init(ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i); + struct two_d_arr *m2 = + two_d_arr_init(ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i); + setZeroes(m1->arr, m1->row_size, m1->col_size); + setZeroes(m2->arr, m2->row_size, m2->col_size); + for (int i = 0; i < m1->row_size; i++) { + for (int j = 0; j < m1->col_size[i]; j++) printf("%d ", m1i[i][j]); // expect: [[1,0,1],[0,0,0],[1,0,1]] printf("\n"); } - for (int i = 0; i < m2.row_size; i++) { - for (int j = 0; j < m2.col_size[i]; j++) + for (int i = 0; i < m2->row_size; i++) { + for (int j = 0; j < m2->col_size[i]; j++) printf("%d ", m2i[i][j]); // expect: [[0,0,0,0],[0,4,5,0],[0,3,1,0]] printf("\n"); } - two_d_arr_free(&m1); - two_d_arr_free(&m2); + two_d_arr_free(m1); + two_d_arr_free(m2); } diff --git a/shifting_letters2.c b/shifting_letters2.c index de20259..39deedc 100644 --- a/shifting_letters2.c +++ b/shifting_letters2.c @@ -38,11 +38,12 @@ int main() { char *s1 = "abc", *s2 = "dztz"; int s1i[3][3] = {{0, 1, 0}, {1, 2, 1}, {0, 2, 1}}, s2i[2][3] = {{0, 0, 0}, {1, 1, 1}}; - struct two_d_arr sh1, sh2; - two_d_arr_init(&sh1, 3, 3, s1i); - two_d_arr_init(&sh1, 2, 3, s2i); - printf("%s\n", shiftingLetters(s1, sh1.arr, sh1.row_size, - sh1.col_size)); // expect: ace - printf("%s\n", shiftingLetters(s2, sh2.arr, sh2.row_size, - sh2.col_size)); // expect: catz + struct two_d_arr *sh1 = two_d_arr_init(3, 3, s1i); + struct two_d_arr *sh2 = two_d_arr_init(2, 3, s2i); + printf("%s\n", shiftingLetters(s1, sh1->arr, sh1->row_size, + sh1->col_size)); // expect: ace + printf("%s\n", shiftingLetters(s2, sh2->arr, sh2->row_size, + sh2->col_size)); // expect: catz + two_d_arr_free(sh1); + two_d_arr_free(sh2); } diff --git a/shopping_offers.c b/shopping_offers.c index bc7b735..4c717b1 100644 --- a/shopping_offers.c +++ b/shopping_offers.c @@ -110,13 +110,16 @@ int main() { int p1[] = {2, 5}, s1i[2][3] = {{3, 0, 5}, {1, 2, 10}}, n1[] = {3, 2}; int p2[] = {2, 3, 4}, s2i[2][4] = {{1, 1, 0, 4}, {2, 2, 1, 9}}, n2[] = {1, 2, 1}; - struct two_d_arr s1, s2; - two_d_arr_init(&s1, ARRAY_SIZE(s1i), ARRAY_SIZE(s1i[0]), s1i); - two_d_arr_init(&s2, ARRAY_SIZE(s2i), ARRAY_SIZE(s2i[0]), s2i); - printf("%d\n", shoppingOffers(p1, ARRAY_SIZE(p1), s1.arr, s1.row_size, - s1.col_size, n1, ARRAY_SIZE(n1))); // expect: 14 - printf("%d\n", shoppingOffers(p2, ARRAY_SIZE(p2), s2.arr, s2.row_size, - s2.col_size, n2, ARRAY_SIZE(n2))); // expect: 11 - two_d_arr_free(&s1); - two_d_arr_free(&s2); + struct two_d_arr *s1 = + two_d_arr_init(ARRAY_SIZE(s1i), ARRAY_SIZE(s1i[0]), s1i); + struct two_d_arr *s2 = + two_d_arr_init(ARRAY_SIZE(s2i), ARRAY_SIZE(s2i[0]), s2i); + printf("%d\n", + shoppingOffers(p1, ARRAY_SIZE(p1), s1->arr, s1->row_size, s1->col_size, + n1, ARRAY_SIZE(n1))); // expect: 14 + printf("%d\n", + shoppingOffers(p2, ARRAY_SIZE(p2), s2->arr, s2->row_size, s2->col_size, + n2, ARRAY_SIZE(n2))); // expect: 11 + two_d_arr_free(s1); + two_d_arr_free(s2); } diff --git a/shortest_path_binary_matrix.c b/shortest_path_binary_matrix.c index 20c8d97..f781466 100644 --- a/shortest_path_binary_matrix.c +++ b/shortest_path_binary_matrix.c @@ -91,17 +91,16 @@ int main() { int g1i[2][2] = {{0, 1}, {1, 0}}; int g2i[3][3] = {{0, 0, 0}, {1, 1, 0}, {1, 1, 0}}; int g3i[3][3] = {{1, 0, 0}, {1, 1, 0}, {1, 1, 0}}; - struct two_d_arr g1, g2, g3; - two_d_arr_init(&g1, 2, 2, g1i); - two_d_arr_init(&g2, 3, 3, g2i); - two_d_arr_init(&g3, 3, 3, g3i); - printf("%d\n", shortestPathBinaryMatrix(g1.arr, g1.row_size, - g1.col_size)); // expect: 2 - printf("%d\n", shortestPathBinaryMatrix(g2.arr, g2.row_size, - g3.col_size)); // expect: 4 - printf("%d\n", shortestPathBinaryMatrix(g3.arr, g3.row_size, - g3.col_size)); // expect: -1 - two_d_arr_free(&g1); - two_d_arr_free(&g2); - two_d_arr_free(&g3); + struct two_d_arr *g1 = two_d_arr_init(2, 2, g1i); + struct two_d_arr *g2 = two_d_arr_init(3, 3, g2i); + struct two_d_arr *g3 = two_d_arr_init(3, 3, g3i); + printf("%d\n", shortestPathBinaryMatrix(g1->arr, g1->row_size, + g1->col_size)); // expect: 2 + printf("%d\n", shortestPathBinaryMatrix(g2->arr, g2->row_size, + g3->col_size)); // expect: 4 + printf("%d\n", shortestPathBinaryMatrix(g3->arr, g3->row_size, + g3->col_size)); // expect: -1 + two_d_arr_free(g1); + two_d_arr_free(g2); + two_d_arr_free(g3); } diff --git a/snakes_ladders.c b/snakes_ladders.c index b7128db..38ac1fe 100644 --- a/snakes_ladders.c +++ b/snakes_ladders.c @@ -70,13 +70,14 @@ int main() { {-1, -1, -1, -1, -1, -1}, {-1, 35, -1, -1, 13, -1}, {-1, -1, -1, -1, -1, -1}, {-1, 15, -1, -1, -1, -1}}; int b2i[2][2] = {{-1, -1}, {-1, 3}}; - struct two_d_arr b1, b2; - two_d_arr_init(&b1, ARRAY_SIZE(b1i), ARRAY_SIZE(b1i[0]), b1i); - two_d_arr_init(&b2, ARRAY_SIZE(b2i), ARRAY_SIZE(b2i[0]), b2i); + struct two_d_arr *b1 = + two_d_arr_init(ARRAY_SIZE(b1i), ARRAY_SIZE(b1i[0]), b1i); + struct two_d_arr *b2 = + two_d_arr_init(ARRAY_SIZE(b2i), ARRAY_SIZE(b2i[0]), b2i); printf("%d\n", - snakesAndLadders(b1.arr, b1.row_size, b1.col_size)); // expect: 4 + snakesAndLadders(b1->arr, b1->row_size, b1->col_size)); // expect: 4 printf("%d\n", - snakesAndLadders(b2.arr, b2.row_size, b2.col_size)); // expect: 1 - two_d_arr_free(&b1); - two_d_arr_free(&b2); + snakesAndLadders(b2->arr, b2->row_size, b2->col_size)); // expect: 1 + two_d_arr_free(b1); + two_d_arr_free(b2); } diff --git a/sort_matrix_by_diagonals.c b/sort_matrix_by_diagonals.c new file mode 100644 index 0000000..2da8ad3 --- /dev/null +++ b/sort_matrix_by_diagonals.c @@ -0,0 +1,93 @@ +// 3446. Sort Matrix by Diagonals +#include "leetcode.h" + +/* + * you are given an 'n x n' square matrix of integers 'grid'. return the matrix + * such that the diagonals in the bottom left triangle including the middle are + * sorted in non-increasing order. the diagonals in the top-right triangle are + * sorted in non-decreasing order. + */ + +int cmp1(const void *a, const void *b) { return (*(int *)a - *(int *)b); } + +int cmp2(const void *a, const void *b) { return (*(int *)b - *(int *)a); } + +int **sortMatrix(int **grid, int gridSize, int *gridColSize, int *returnSize, + int **returnColumnSizes) { + *returnSize = gridSize; + *returnColumnSizes = gridColSize; + int n = gridSize, m = gridColSize[0]; + int diag_cnt = n + m - 1; + int *sizes = (int *)calloc(diag_cnt, sizeof(int)); + int **diags = (int **)malloc(diag_cnt * sizeof(int *)); + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + sizes[i - j + m - 1]++; + for (int k = 0; k < diag_cnt; k++) { + diags[k] = (int *)malloc(sizes[k] * sizeof(int)); + sizes[k] = 0; + } + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + diags[i - j + m - 1][sizes[i - j + m - 1]++] = grid[i][j]; + for (int k = 0; k < diag_cnt; k++) { + if (k < m - 1) + qsort(diags[k], sizes[k], sizeof(int), cmp1); + else + qsort(diags[k], sizes[k], sizeof(int), cmp2); + sizes[k] = 0; + } + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + grid[i][j] = diags[i - j + m - 1][sizes[i - j + m - 1]++]; + for (int i = 0; i < diag_cnt; i++) + free(diags[i]); + free(diags); + free(sizes); + return grid; +} + +int main() { + int g1i[3][3] = {{1, 7, 3}, {9, 8, 2}, {4, 5, 6}}, rs1, *rcs1; + int g2i[2][2] = {{0, 1}, {1, 2}}, rs2, *rcs2; + int g3i[1][1] = {{1}}, rs3, *rcs3; + struct two_d_arr *g1 = + two_d_arr_init(ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); + struct two_d_arr *g2 = + two_d_arr_init(ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); + struct two_d_arr *g3 = + two_d_arr_init(ARRAY_SIZE(g3i), ARRAY_SIZE(g3i[0]), g3i); + int **sm1 = sortMatrix(g1->arr, g1->row_size, g1->col_size, &rs1, &rcs1); + int **sm2 = sortMatrix(g2->arr, g2->row_size, g2->col_size, &rs2, &rcs2); + int **sm3 = sortMatrix(g3->arr, g3->row_size, g3->col_size, &rs3, &rcs3); + for (int i = 0; i < rs1; i++) { + for (int j = 0; j < rcs1[i]; j++) + printf("%d ", sm1[i][j]); // expect: [[8,2,3],[9,6,7],[4,5,1]] + printf("\n"); + } + for (int i = 0; i < rs2; i++) { + for (int j = 0; j < rcs2[i]; j++) + printf("%d ", sm2[i][j]); // expect: [[2,1],[1,0]] + printf("\n"); + } + for (int i = 0; i < rs3; i++) { + for (int j = 0; j < rcs3[i]; j++) + printf("%d ", sm3[i][j]); // expect: [[1]] + printf("\n"); + } + for (int i = 0; i < rs1; i++) + free(sm1[i]); + free(sm1); + for (int i = 0; i < rs2; i++) + free(sm2[i]); + free(sm2); + for (int i = 0; i < rs3; i++) + free(sm3[i]); + free(sm3); + free(rcs1); + free(rcs2); + free(rcs3); + two_d_arr_free(g1); + two_d_arr_free(g2); + two_d_arr_free(g3); +} diff --git a/sort_matrix_by_diagonals.py b/sort_matrix_by_diagonals.py new file mode 100644 index 0000000..1aee13e --- /dev/null +++ b/sort_matrix_by_diagonals.py @@ -0,0 +1,43 @@ +# 3446. Sort Matrix by Diagonals +import heapq + +""" +you are given an 'n x n' square matrix of integers 'grid'. return the matrix +such that the diagonals in the bottom left triangle including the middle are +sorted in non-increasing order. the diagonals in the top-right triangle are +sorted in non-decreasing order. +""" + + +class Solution(object): + def sortMatrix(self, grid): + """ + :type grid: List[List[int]] + :rtype: List[List[int]] + """ + n, m = len(grid), len(grid[0]) + diags = {} + for i in range(n): + for j in range(m): + key = i - j + if key not in diags: + diags[key] = [] + if key < 0: + heapq.heappush(diags[key], grid[i][j]) + else: + heapq.heappush(diags[key], -grid[i][j]) + for i in range(n): + for j in range(m): + key = i - j + if key < 0: + grid[i][j] = heapq.heappop(diags[key]) + else: + grid[i][j] = -heapq.heappop(diags[key]) + return grid + + +if __name__ == "__main__": + obj = Solution() + print(obj.sortMatrix(grid=[[1, 7, 3], [9, 8, 2], [4, 5, 6]])) + print(obj.sortMatrix(grid=[[0, 1], [1, 2]])) + print(obj.sortMatrix(grid=[[1]])) diff --git a/special_array2.c b/special_array2.c index b929212..665e948 100644 --- a/special_array2.c +++ b/special_array2.c @@ -30,13 +30,12 @@ bool *isArraySpecial(int *nums, int numsSize, int **queries, int queriesSize, int main() { int n1[] = {3, 4, 1, 2, 6}, q1i[1][2] = {{0, 4}}, rs1; int n2[] = {4, 3, 1, 6}, q2i[2][2] = {{0, 2}, {2, 3}}, rs2; - struct two_d_arr q1, q2; - two_d_arr_init(&q1, 1, 2, q1i); - two_d_arr_init(&q2, 2, 2, q2i); - bool *ias1 = isArraySpecial(n1, ARRAY_SIZE(n1), q1.arr, q1.row_size, - q1.col_size, &rs1); - bool *ias2 = isArraySpecial(n2, ARRAY_SIZE(n2), q2.arr, q2.row_size, - q2.col_size, &rs2); + struct two_d_arr *q1 = two_d_arr_init(1, 2, q1i); + struct two_d_arr *q2 = two_d_arr_init(2, 2, q2i); + bool *ias1 = isArraySpecial(n1, ARRAY_SIZE(n1), q1->arr, q1->row_size, + q1->col_size, &rs1); + bool *ias2 = isArraySpecial(n2, ARRAY_SIZE(n2), q2->arr, q2->row_size, + q2->col_size, &rs2); for (int i = 0; i < rs1; i++) printf("%d ", ias1[i]); // expect: 0 printf("\n"); @@ -44,5 +43,6 @@ int main() { printf("%d ", ias2[i]); // expect: 0 1 printf("\n"); free(ias1), free(ias2); - two_d_arr_free(&q1), two_d_arr_free(&q2); + two_d_arr_free(q1); + two_d_arr_free(q2); } diff --git a/stamping_the_grid.c b/stamping_the_grid.c index e874acc..f2f25eb 100644 --- a/stamping_the_grid.c +++ b/stamping_the_grid.c @@ -77,13 +77,14 @@ int main() { int g1i[6][4] = { {1, 0, 0, 0}, {1, 0, 0, 0}, {1, 0, 0, 0}, {1, 0, 0, 0}, {1, 0, 0, 0}}; int g2i[4][4] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; - struct two_d_arr g1, g2; - two_d_arr_init(&g1, ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); - two_d_arr_init(&g2, ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); - printf("%d\n", - possibleToStamp(g1.arr, g1.row_size, g1.col_size, 4, 3)); // expect: 1 - printf("%d\n", - possibleToStamp(g2.arr, g2.row_size, g2.col_size, 2, 2)); // expect: 0 - two_d_arr_free(&g1); - two_d_arr_free(&g2); + struct two_d_arr *g1 = + two_d_arr_init(ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i); + struct two_d_arr *g2 = + two_d_arr_init(ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i); + printf("%d\n", possibleToStamp(g1->arr, g1->row_size, g1->col_size, 4, + 3)); // expect: 1 + printf("%d\n", possibleToStamp(g2->arr, g2->row_size, g2->col_size, 2, + 2)); // expect: 0 + two_d_arr_free(g1); + two_d_arr_free(g2); } diff --git a/substring_xor_queries.c b/substring_xor_queries.c index 4c2a6d2..bef8e7f 100644 --- a/substring_xor_queries.c +++ b/substring_xor_queries.c @@ -88,19 +88,21 @@ int **substringXorQueries(char *s, int **queries, int queriesSize, int main() { char *s1 = "101101", *s2 = "0101", *s3 = "1"; int q1i[2][2] = {{0, 5}, {1, 2}}, q2i[1][2] = {{12, 8}}, q3i[1][2] = {{4, 5}}; - struct two_d_arr q1, q2, q3; - two_d_arr_init(&q1, ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); - two_d_arr_init(&q2, ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); - two_d_arr_init(&q3, ARRAY_SIZE(q3i), ARRAY_SIZE(q3i[0]), q3i); + struct two_d_arr *q1 = + two_d_arr_init(ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); + struct two_d_arr *q2 = + two_d_arr_init(ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); + struct two_d_arr *q3 = + two_d_arr_init(ARRAY_SIZE(q3i), ARRAY_SIZE(q3i[0]), q3i); int rs1, *rcs1, - **sxq1 = substringXorQueries(s1, q1.arr, q1.row_size, q1.col_size, &rs1, - &rcs1); + **sxq1 = substringXorQueries(s1, q1->arr, q1->row_size, q1->col_size, + &rs1, &rcs1); int rs2, *rcs2, - **sxq2 = substringXorQueries(s2, q2.arr, q2.row_size, q2.col_size, &rs2, - &rcs2); + **sxq2 = substringXorQueries(s2, q2->arr, q2->row_size, q2->col_size, + &rs2, &rcs2); int rs3, *rcs3, - **sxq3 = substringXorQueries(s3, q3.arr, q3.row_size, q3.col_size, &rs3, - &rcs3); + **sxq3 = substringXorQueries(s3, q3->arr, q3->row_size, q3->col_size, + &rs3, &rcs3); for (int i = 0; i < rs1; i++) { for (int j = 0; j < rcs1[i]; j++) printf("%d ", sxq1[i][j]); // expect: [[0,2],[2,3]] @@ -131,7 +133,7 @@ int main() { free(rcs1); free(rcs2); free(rcs3); - two_d_arr_free(&q1); - two_d_arr_free(&q2); - two_d_arr_free(&q3); + two_d_arr_free(q1); + two_d_arr_free(q2); + two_d_arr_free(q3); } diff --git a/transform_to_chessboard.c b/transform_to_chessboard.c index efe2031..8bd9f08 100644 --- a/transform_to_chessboard.c +++ b/transform_to_chessboard.c @@ -41,17 +41,16 @@ int main() { int b1i[4][4] = {{0, 1, 1, 0}, {0, 1, 1, 0}, {1, 0, 0, 1}, {1, 0, 0, 1}}; int b2i[2][2] = {{0, 1}, {1, 0}}; int b3i[2][2] = {{1, 0}, {1, 0}}; - struct two_d_arr b1, b2, b3; - two_d_arr_init(&b1, 4, 4, b1i); - two_d_arr_init(&b2, 2, 2, b2i); - two_d_arr_init(&b3, 2, 2, b3i); + struct two_d_arr *b1 = two_d_arr_init(4, 4, b1i); + struct two_d_arr *b2 = two_d_arr_init(2, 2, b2i); + struct two_d_arr *b3 = two_d_arr_init(2, 2, b3i); printf("%d\n", - movesToChessboard(b1.arr, b2.row_size, b3.col_size)); // expect: 2 + movesToChessboard(b1->arr, b2->row_size, b3->col_size)); // expect: 2 printf("%d\n", - movesToChessboard(b2.arr, b2.row_size, b2.col_size)); // expect: 0 + movesToChessboard(b2->arr, b2->row_size, b2->col_size)); // expect: 0 printf("%d\n", - movesToChessboard(b3.arr, b3.row_size, b3.col_size)); // expect: -1 - two_d_arr_free(&b1); - two_d_arr_free(&b2); - two_d_arr_free(&b3); + movesToChessboard(b3->arr, b3->row_size, b3->col_size)); // expect: -1 + two_d_arr_free(b1); + two_d_arr_free(b2); + two_d_arr_free(b3); } diff --git a/trapping_rain_water2.c b/trapping_rain_water2.c index adde260..fdedd1e 100644 --- a/trapping_rain_water2.c +++ b/trapping_rain_water2.c @@ -85,11 +85,15 @@ int main() { {3, 2, 1, 2, 3}, {3, 2, 2, 2, 3}, {3, 3, 3, 3, 3}}; - struct two_d_arr hm1, hm2, hm3; - two_d_arr_init(&hm1, ARRAY_SIZE(hm1i), ARRAY_SIZE(hm1i[0]), hm1i); - two_d_arr_init(&hm2, ARRAY_SIZE(hm2i), ARRAY_SIZE(hm2i[0]), hm2i); + struct two_d_arr *hm1 = + two_d_arr_init(ARRAY_SIZE(hm1i), ARRAY_SIZE(hm1i[0]), hm1i); + struct two_d_arr *hm2 = + two_d_arr_init(ARRAY_SIZE(hm2i), ARRAY_SIZE(hm2i[0]), hm2i); printf("%d\n", - trapRainWater(hm1.arr, hm1.row_size, hm1.col_size)); // expect: 4 + trapRainWater(hm1->arr, hm1->row_size, hm1->col_size)); // expect: 4 printf("%d\n", - trapRainWater(hm2.arr, hm2.row_size, hm2.col_size)); // expect: 10 + trapRainWater(hm2->arr, hm2->row_size, hm2->col_size)); // expect: 10 + + two_d_arr_free(hm1); + two_d_arr_free(hm2); } diff --git a/two_best_nonoverlap_event.c b/two_best_nonoverlap_event.c index f779dd8..6dfe90e 100644 --- a/two_best_nonoverlap_event.c +++ b/two_best_nonoverlap_event.c @@ -72,14 +72,16 @@ int main() { int e1i[3][3] = {{1, 3, 2}, {4, 5, 2}, {2, 4, 3}}, e2i[3][3] = {{1, 3, 2}, {4, 5, 2}, {1, 5, 5}}, e3i[3][3] = {{1, 5, 3}, {1, 5, 1}, {6, 6, 5}}; - struct two_d_arr e1, e2, e3; - two_d_arr_init(&e1, 3, 3, e1i); - two_d_arr_init(&e2, 3, 3, e2i); - two_d_arr_init(&e3, 3, 3, e3i); - printf("%d\n", maxTwoEvents(e1.arr, e1.row_size, e1.col_size)); // expect: 4 - printf("%d\n", maxTwoEvents(e2.arr, e2.row_size, e2.col_size)); // expect: 5 - printf("%d\n", maxTwoEvents(e3.arr, e3.row_size, e3.col_size)); // expect: 8 - two_d_arr_free(&e1); - two_d_arr_free(&e2); - two_d_arr_free(&e3); + struct two_d_arr *e1 = two_d_arr_init(3, 3, e1i); + struct two_d_arr *e2 = two_d_arr_init(3, 3, e2i); + struct two_d_arr *e3 = two_d_arr_init(3, 3, e3i); + printf("%d\n", + maxTwoEvents(e1->arr, e1->row_size, e1->col_size)); // expect: 4 + printf("%d\n", + maxTwoEvents(e2->arr, e2->row_size, e2->col_size)); // expect: 5 + printf("%d\n", + maxTwoEvents(e3->arr, e3->row_size, e3->col_size)); // expect: 8 + two_d_arr_free(e1); + two_d_arr_free(e2); + two_d_arr_free(e3); } diff --git a/video_stitching.c b/video_stitching.c index 1fd83db..fe41244 100644 --- a/video_stitching.c +++ b/video_stitching.c @@ -62,17 +62,19 @@ int main() { int c3i[16][2] = {{0, 1}, {6, 8}, {0, 2}, {5, 6}, {0, 4}, {0, 3}, {6, 7}, {1, 3}, {4, 7}, {1, 4}, {2, 5}, {2, 6}, {3, 4}, {4, 5}, {5, 7}, {6, 9}}; - struct two_d_arr c1, c2, c3; - two_d_arr_init(&c1, ARRAY_SIZE(c1i), ARRAY_SIZE(c1i[0]), c1i); - two_d_arr_init(&c2, ARRAY_SIZE(c2i), ARRAY_SIZE(c2i[0]), c2i); - two_d_arr_init(&c3, ARRAY_SIZE(c3i), ARRAY_SIZE(c3i[0]), c3i); + struct two_d_arr *c1 = + two_d_arr_init(ARRAY_SIZE(c1i), ARRAY_SIZE(c1i[0]), c1i); + struct two_d_arr *c2 = + two_d_arr_init(ARRAY_SIZE(c2i), ARRAY_SIZE(c2i[0]), c2i); + struct two_d_arr *c3 = + two_d_arr_init(ARRAY_SIZE(c3i), ARRAY_SIZE(c3i[0]), c3i); printf("%d\n", - videoStitching(c1.arr, c1.row_size, c1.col_size, 3)); // expect: 3 + videoStitching(c1->arr, c1->row_size, c1->col_size, 3)); // expect: 3 printf("%d\n", - videoStitching(c2.arr, c2.row_size, c2.col_size, 5)); // expect: -1 + videoStitching(c2->arr, c2->row_size, c2->col_size, 5)); // expect: -1 printf("%d\n", - videoStitching(c3.arr, c3.row_size, c3.col_size, 9)); // expect: 3 - two_d_arr_free(&c1); - two_d_arr_free(&c2); - two_d_arr_free(&c3); + videoStitching(c3->arr, c3->row_size, c3->col_size, 9)); // expect: 3 + two_d_arr_free(c1); + two_d_arr_free(c2); + two_d_arr_free(c3); } diff --git a/zero_array_transformation1.c b/zero_array_transformation1.c index 45f388c..089c0ff 100644 --- a/zero_array_transformation1.c +++ b/zero_array_transformation1.c @@ -33,13 +33,14 @@ bool isZeroArray(int *nums, int numsSize, int **queries, int queriesSize, int main() { int n1[] = {1, 0, 1}, q1i[1][2] = {{0, 2}}; int n2[] = {4, 3, 2, 1}, q2i[2][2] = {{1, 3}, {0, 2}}; - struct two_d_arr q1, q2; - two_d_arr_init(&q1, ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); - two_d_arr_init(&q2, ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); - printf("%d\n", isZeroArray(n1, ARRAY_SIZE(n1), q1.arr, q1.row_size, - q1.col_size)); // expect: 1 - printf("%d\n", isZeroArray(n2, ARRAY_SIZE(n2), q2.arr, q2.row_size, - q2.col_size)); // expect: 0 - two_d_arr_free(&q1); - two_d_arr_free(&q2); + struct two_d_arr *q1 = + two_d_arr_init(ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); + struct two_d_arr *q2 = + two_d_arr_init(ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); + printf("%d\n", isZeroArray(n1, ARRAY_SIZE(n1), q1->arr, q1->row_size, + q1->col_size)); // expect: 1 + printf("%d\n", isZeroArray(n2, ARRAY_SIZE(n2), q2->arr, q2->row_size, + q2->col_size)); // expect: 0 + two_d_arr_free(q1); + two_d_arr_free(q2); } diff --git a/zero_array_transformation2.c b/zero_array_transformation2.c index 4532469..769421d 100644 --- a/zero_array_transformation2.c +++ b/zero_array_transformation2.c @@ -59,13 +59,14 @@ int minZeroArray(int *nums, int numsSize, int **queries, int queriesSize, int main() { int n1[] = {2, 0, 2}, q1i[3][3] = {{0, 2, 1}, {0, 2, 1}, {1, 1, 3}}; int n2[] = {4, 3, 2, 1}, q2i[2][3] = {{1, 3, 2}, {0, 2, 1}}; - struct two_d_arr q1, q2; - two_d_arr_init(&q1, ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); - two_d_arr_init(&q2, ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); - printf("%d\n", minZeroArray(n1, ARRAY_SIZE(n1), q1.arr, q1.row_size, - q1.col_size)); // expect: 2 - printf("%d\n", minZeroArray(n2, ARRAY_SIZE(n2), q2.arr, q2.row_size, - q2.col_size)); // expect: -1 - two_d_arr_free(&q1); - two_d_arr_free(&q2); + struct two_d_arr *q1 = + two_d_arr_init(ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); + struct two_d_arr *q2 = + two_d_arr_init(ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); + printf("%d\n", minZeroArray(n1, ARRAY_SIZE(n1), q1->arr, q1->row_size, + q1->col_size)); // expect: 2 + printf("%d\n", minZeroArray(n2, ARRAY_SIZE(n2), q2->arr, q2->row_size, + q2->col_size)); // expect: -1 + two_d_arr_free(q1); + two_d_arr_free(q2); } diff --git a/zero_array_transformation3.c b/zero_array_transformation3.c index b966099..7f323cb 100644 --- a/zero_array_transformation3.c +++ b/zero_array_transformation3.c @@ -115,13 +115,14 @@ int maxRemoval(int *nums, int numsSize, int **queries, int queriesSize, int main() { int n1[] = {2, 0, 2}, q1i[3][2] = {{0, 2}, {0, 2}, {1, 1}}; int n2[] = {1, 1, 1, 1}, q2i[4][2] = {{1, 3}, {0, 2}, {1, 3}, {1, 2}}; - struct two_d_arr q1, q2; - two_d_arr_init(&q1, ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); - two_d_arr_init(&q2, ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); - printf("%d\n", maxRemoval(n1, ARRAY_SIZE(n1), q1.arr, q1.row_size, - q1.col_size)); // expect: 1 - printf("%d\n", maxRemoval(n2, ARRAY_SIZE(n2), q2.arr, q2.row_size, - q2.col_size)); // expect: 2 - two_d_arr_free(&q1); - two_d_arr_free(&q2); + struct two_d_arr *q1 = + two_d_arr_init(ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); + struct two_d_arr *q2 = + two_d_arr_init(ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); + printf("%d\n", maxRemoval(n1, ARRAY_SIZE(n1), q1->arr, q1->row_size, + q1->col_size)); // expect: 2 + printf("%d\n", maxRemoval(n2, ARRAY_SIZE(n2), q2->arr, q2->row_size, + q2->col_size)); // expect: 1 + two_d_arr_free(q1); + two_d_arr_free(q2); } |