Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Solve Problem 2257. Count Unguarded Cells in the Grid in C #2210

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
3 changes: 3 additions & 0 deletions old-problems/2257/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
29 changes: 29 additions & 0 deletions old-problems/2257/c/interface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <vector>

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<std::vector<int>> guards,
std::vector<std::vector<int>> walls) {
std::vector<int*> guardsDatas(guards.size());
std::vector<int> 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<int*> wallsDatas(walls.size());
std::vector<int> 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());
}
49 changes: 49 additions & 0 deletions old-problems/2257/c/solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <stdlib.h>
#include <string.h>

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;
}
1 change: 1 addition & 0 deletions old-problems/2257/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ types:
output: int

solutions:
c:
cpp:
function: countUnguarded

Expand Down