Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Number of islands #213

Merged
merged 4 commits into from
Mar 19, 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
83 changes: 83 additions & 0 deletions leetcode-cc/NumOfIslands.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <unordered_set>
#include <utility>

#include "PairHash.h"
#include "TestHelper.h"
#include "problem.h"
#include "solution.h"

using namespace std;

IMPLEMENT_PROBLEM_CLASS(
PNumOfIslands, 200, DIFFI_MEDIUM, TOPIC_ALGORITHMS, "Number of Islands",
"Given an m x n 2D binary grid grid which represents a map of '1's (land) "
"and '0's (water), return the number of islands.",
{"Matrix && DFS"});

class SNumOfIslands : public ISolution {
public:
size_t problemId() const override { return 200; }
string name() const override {
return ("Solution for " + string("Number of Islands"));
}
string location() const override { return __FILE_NAME__; }
int test() const override {
return testHelper<vector<vector<char>>, int>(
{{
{'1', '1', '1', '1', '0'},
{'1', '1', '0', '1', '0'},
{'1', '1', '0', '0', '0'},
{'0', '0', '0', '0', '0'},
},
{
{'1', '1', '0', '0', '0'},
{'1', '1', '0', '0', '0'},
{'0', '0', '1', '0', '0'},
{'0', '0', '0', '1', '1'},
}},
{1, 3}, [this](auto input) { return this->numIslands(input); });
};
int benchmark() const override { return 0; }

private:
int numIslands(vector<vector<char>>& grid) const {
int m = grid.size();
int n = grid[0].size();

pair<int, int> p = {0, 0};

while (p.first < m && p.second < n) {
if (grid[p.first][p.second] == '1') break;
p = nextPair(p, m, n);
}

int res = 0;
unordered_set<pair<int, int>, pair_hash> set = {};

while (p.first < m && p.second < n) {
if (!set.contains(p) && grid[p.first][p.second] == '1') {
dfs(grid, p.first, p.second, set);
res++;
}
p = nextPair(p, m, n);
}

return res;
}

static void dfs(vector<vector<char>>& grid, int row, int col,
unordered_set<pair<int, int>, pair_hash>& set) {
if (!set.contains({row, col}) && grid[row][col] == '1') {
set.insert({row, col});
if (row > 0) dfs(grid, row - 1, col, set);
if (row < grid.size() - 1) dfs(grid, row + 1, col, set);
if (col > 0) dfs(grid, row, col - 1, set);
if (col < grid[0].size() - 1) dfs(grid, row, col + 1, set);
}
}

static pair<int, int> nextPair(pair<int, int> p, int m, int n) {
return (p.second == n - 1) ? make_pair(p.first + 1, 0)
: make_pair(p.first, p.second + 1);
}
};
9 changes: 9 additions & 0 deletions leetcode-cc/PairHash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <cstddef>

struct pair_hash {
inline std::size_t operator()(const std::pair<int, int>& v) const {
return v.first * 31 + v.second;
}
};
7 changes: 1 addition & 6 deletions leetcode-cc/SurroundedRegions.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
#include <unordered_set>

#include "PairHash.h"
#include "TestHelper.h"
#include "problem.h"
#include "solution.h"

using namespace std;

struct pair_hash {
inline size_t operator()(const pair<int, int>& v) const {
return v.first * 31 + v.second;
}
};

IMPLEMENT_PROBLEM_CLASS(
PSurroundedRegions, 130, DIFFI_MEDIUM, TOPIC_ALGORITHMS,
"Surrounded Regions",
Expand Down
6 changes: 6 additions & 0 deletions runtime-cc/src/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#include "../leetcode-cc/MinNumStepsMakeTwoStrAnagram.hpp"
#include "../leetcode-cc/MinStack.hpp"
#include "../leetcode-cc/NextPermutation.hpp"
#include "../leetcode-cc/NumOfIslands.hpp"
#include "../leetcode-cc/NumOfOneBits.hpp"
#include "../leetcode-cc/OutOfBoundaryPaths.hpp"
#include "../leetcode-cc/PalindromePartitioning.hpp"
Expand Down Expand Up @@ -807,5 +808,10 @@ const int registerAll(std::shared_ptr<Container> handle) {
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SNumOfOneBits>(); });

handle->registerProblem(
[]() -> ArcProblem { return std::make_shared<PNumOfIslands>(); });
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SNumOfIslands>(); });

return 0;
}
Loading