diff --git a/README.md b/README.md index f560c473e..b4c2784da 100644 --- a/README.md +++ b/README.md @@ -462,7 +462,7 @@ LeetSpace serves as a dedicated workspace and archive for my [LeetCode](https:// | [2232. Minimize Result by Adding Parentheses to Expression](https://leetcode.com/problems/minimize-result-by-adding-parentheses-to-expression/) | Medium | [C](./old-problems/2232/c/solution.c) [C++](./old-problems/2232/solution.cpp) | | [2235. Add Two Integers](https://leetcode.com/problems/add-two-integers) | Easy | [C](./old-problems/2235/c/solution.c) [C++](./old-problems/2235/solution.cpp) | | [2251. Number of Flowers in Full Bloom](https://leetcode.com/problems/number-of-flowers-in-full-bloom) | Hard | [C++](./problems/2251/solution.cpp) | -| [2257. Count Unguarded Cells in the Grid](https://leetcode.com/problems/count-unguarded-cells-in-the-grid/) | Medium | [C++](./old-problems/2257/solution.cpp) | +| [2257. Count Unguarded Cells in the Grid](https://leetcode.com/problems/count-unguarded-cells-in-the-grid/) | Medium | [C](./old-problems/2257/c/solution.c) [C++](./old-problems/2257/solution.cpp) | | [2264. Largest 3-Same-Digit Number in String](https://leetcode.com/problems/largest-3-same-digit-number-in-string/) | Easy | [C](./old-problems/2264/c/solution.c) [C++](./old-problems/2264/solution.cpp) | | [2275. Largest Combination With Bitwise AND Greater Than Zero](https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/) | Medium | [C](./old-problems/2275/c/solution.c) [C++](./old-problems/2275/solution.cpp) | | [2285. Maximum Total Importance of Roads](https://leetcode.com/problems/maximum-total-importance-of-roads/) | Medium | [C](./old-problems/2285/c/solution.c) [C++](./old-problems/2285/solution.cpp) | diff --git a/old-problems/2257/CMakeLists.txt b/old-problems/2257/CMakeLists.txt index 7abee69ef..597f5d1d1 100644 --- a/old-problems/2257/CMakeLists.txt +++ b/old-problems/2257/CMakeLists.txt @@ -1,2 +1,5 @@ +add_c_solution(c_solution c/interface.cpp c/solution.c) + get_dir_name(id) add_problem_test(test-${id} test.yaml) +target_link_libraries(test-${id} PRIVATE ${c_solution}) diff --git a/old-problems/2257/c/interface.cpp b/old-problems/2257/c/interface.cpp new file mode 100644 index 000000000..5ca2a8da3 --- /dev/null +++ b/old-problems/2257/c/interface.cpp @@ -0,0 +1,29 @@ +#include + +extern "C" { +int countUnguarded( + int m, int n, int** guards, int guardsSize, int* guardsColSize, + int** walls, int wallsSize, int* wallsColSize); +} + +int solution_c( + int m, int n, std::vector> guards, + std::vector> walls) { + std::vector guardsDatas(guards.size()); + std::vector guardsSizes(guards.size()); + for (std::size_t i{0}; i < guards.size(); ++i) { + guardsDatas[i] = guards[i].data(); + guardsSizes[i] = guards[i].size(); + } + + std::vector wallsDatas(walls.size()); + std::vector wallsSizes(walls.size()); + for (std::size_t i{0}; i < walls.size(); ++i) { + wallsDatas[i] = walls[i].data(); + wallsSizes[i] = walls[i].size(); + } + + return countUnguarded( + m, n, guardsDatas.data(), guardsDatas.size(), guardsSizes.data(), + wallsDatas.data(), wallsDatas.size(), wallsSizes.data()); +} diff --git a/old-problems/2257/c/solution.c b/old-problems/2257/c/solution.c new file mode 100644 index 000000000..9e2be31da --- /dev/null +++ b/old-problems/2257/c/solution.c @@ -0,0 +1,49 @@ +#include +#include + +int countUnguarded( + int m, int n, int** guards, int guardsSize, int* guardsColSize, + int** walls, int wallsSize, int* wallsColSize) { + (void)guardsColSize; + (void)wallsColSize; + + char* cells = malloc(m * n * sizeof(char)); + memset(cells, 0, m * n * sizeof(char)); + + for (int i = 0; i < guardsSize; ++i) { + cells[guards[i][0] * n + guards[i][1]] = 1; + } + + for (int i = 0; i < wallsSize; ++i) { + cells[walls[i][0] * n + walls[i][1]] = 2; + } + + for (int i = 0; i < guardsSize; ++i) { + for (int j = guards[i][0] + 1; j < m; ++j) { + if (cells[j * n + guards[i][1]] > 0) break; + cells[j * n + guards[i][1]] = -1; + } + + for (int j = guards[i][0] - 1; j >= 0; --j) { + if (cells[j * n + guards[i][1]] > 0) break; + cells[j * n + guards[i][1]] = -1; + } + + for (int j = guards[i][1] + 1; j < n; ++j) { + if (cells[guards[i][0] * n + j] > 0) break; + cells[guards[i][0] * n + j] = -1; + } + + for (int j = guards[i][1] - 1; j >= 0; --j) { + if (cells[guards[i][0] * n + j] > 0) break; + cells[guards[i][0] * n + j] = -1; + } + } + + int unguarded = 0; + for (int i = 0; i < m * n; ++i) { + if (cells[i] == 0) ++unguarded; + } + + return unguarded; +} diff --git a/old-problems/2257/test.yaml b/old-problems/2257/test.yaml index 207daff54..f906eddce 100644 --- a/old-problems/2257/test.yaml +++ b/old-problems/2257/test.yaml @@ -9,6 +9,7 @@ types: output: int solutions: + c: cpp: function: countUnguarded