Problem solution · C++

Making A Large Island

Making A Large Island: a C++ solution using hash-based lookup. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Hash-based lookup
Source
walkccc LeetCode Solutions
Length
55 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Hash-based lookup

For Making A Large Island, the implementation stores previously seen values or frequencies in a hash table for direct membership and lookup operations.

  1. Decide the key that represents the information needed later.
  2. Update its count or stored state while scanning the input.
  3. Use constant-time expected lookups to detect matches or assemble the result.

Code notes

  • 55 lines of C++ from the credited upstream file 827.cpp.
  • The implementation visibly relies on sequence storage, hash lookup.
  • 5 loop blocks detected.

Complexity

Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMaking A Large Island · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  int largestIsland(vector<vector<int>>& grid) {    const int m = grid.size();    const int n = grid[0].size();    int maxSize = 0;    // sizes[i] := the size of the i-th connected component (starting from 2)    vector<int> sizes{0, 0};     // For each 1 in the grid, paint all the connected 1s with the next    // available color (2, 3, and so on). Also, remember the size of the island    // we just painted with that color.    for (int i = 0; i < m; ++i)      for (int j = 0; j < n; ++j)        if (grid[i][j] == 1)          sizes.push_back(paint(grid, i, j, sizes.size()));  // Paint 2, 3, ...     for (int i = 0; i < m; ++i)      for (int j = 0; j < n; ++j)        if (grid[i][j] == 0) {          const unordered_set<int> neighborIds{              getId(grid, i + 1, j), getId(grid, i - 1, j),              getId(grid, i, j + 1), getId(grid, i, j - 1)};          maxSize = max(maxSize, 1 + getSize(neighborIds, sizes));        }     return maxSize == 0 ? m * n : maxSize;  }  private:  int paint(vector<vector<int>>& grid, int i, int j, int id) {    if (i < 0 || i == grid.size() || j < 0 || j == grid[0].size())      return 0;    if (grid[i][j] != 1)      return 0;    grid[i][j] = id;  // grid[i][j] is part of the id-th connected component.    return 1 + paint(grid, i + 1, j, id) + paint(grid, i - 1, j, id) +           paint(grid, i, j + 1, id) + paint(grid, i, j - 1, id);  }   // Gets the id of grid[i][j] and returns 0 if it's out-of-bounds.  int getId(const vector<vector<int>>& grid, int i, int j) {    if (i < 0 || i == grid.size() || j < 0 || j == grid[0].size())      return 0;  // Invalid    return grid[i][j];  }   int getSize(const unordered_set<int>& neighborIds, const vector<int>& sizes) {    int size = 0;    for (const int neighborId : neighborIds)      size += sizes[neighborId];    return size;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗