|
| 1 | +#include <bits/stdc++.h> |
| 2 | +#include <gtest/gtest.h> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | + |
| 6 | +//// START |
| 7 | +/* |
| 8 | +## Making A Large Island |
| 9 | + |
| 10 | +*/ |
| 11 | + |
| 12 | + |
| 13 | +class Solution { |
| 14 | + public: |
| 15 | + int largestIsland(vector<vector<int>> &grid) { |
| 16 | + unordered_map<int, bool> globalVisited; |
| 17 | + unordered_map<int, int> position2index, islandArea; |
| 18 | + R = grid.size(), C = grid[0].size(); |
| 19 | + int islandIndex = 0; |
| 20 | + for (int r = 0; r < R; r++) { |
| 21 | + for (int c = 0; c < C; c++) { |
| 22 | + if (grid[r][c] == 0)continue; |
| 23 | + if (globalVisited[position(r, c)]) continue; |
| 24 | + islandIndex++; |
| 25 | + |
| 26 | + globalVisited[position(r, c)] = true; |
| 27 | + position2index[position(r, c)] = islandIndex; |
| 28 | + |
| 29 | + queue<pair<int, int>> qq; |
| 30 | + qq.push(make_pair(r, c)); |
| 31 | + while (!qq.empty()) { |
| 32 | + auto now = qq.front(); |
| 33 | + qq.pop(); |
| 34 | + |
| 35 | + islandArea[islandIndex]++; |
| 36 | + |
| 37 | + for (auto di : directions) { |
| 38 | + int x = now.first + di[0]; |
| 39 | + int y = now.second + di[1]; |
| 40 | + if (x >= 0 && x < R && y >= 0 && y < C && grid[x][y] == 1 |
| 41 | + && !globalVisited[position(x, y)]) { |
| 42 | + globalVisited[position(x, y)] = true; |
| 43 | + position2index[position(x, y)] = islandIndex; |
| 44 | + qq.push(make_pair(x, y)); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + int maxArea = 0; |
| 51 | + for (auto m : islandArea) maxArea = max(maxArea, m.second); |
| 52 | + for (int r = 0; r < R; r++) { |
| 53 | + for (int c = 0; c < C; c++) { |
| 54 | + if (grid[r][c] == 0) { |
| 55 | + unordered_map<int, int> area; |
| 56 | + for (auto di: directions) { |
| 57 | + int x = di[0] + r,y = di[1] + c; |
| 58 | + if (x >= 0 && x < R && y >= 0 && y < C && grid[x][y] == 1) { |
| 59 | + int island = position2index[position(x, y)]; |
| 60 | + area[island] = islandArea[island]; |
| 61 | + } |
| 62 | + } |
| 63 | + int s = 0; |
| 64 | + for (auto ar : area) s += ar.second; |
| 65 | + maxArea = max(maxArea, s + 1); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + return maxArea; |
| 70 | + } |
| 71 | + private: |
| 72 | + vector<vector<int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1},}; |
| 73 | + int R = 0, C = 0; |
| 74 | + int position(int x, int y) { |
| 75 | + return x * 500 + y; |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +//// END |
| 80 | +struct T { |
| 81 | + |
| 82 | +}; |
| 83 | + |
| 84 | +TEST(Solution, test) { |
| 85 | + T ts[] = { |
| 86 | + { |
| 87 | + |
| 88 | + }, |
| 89 | + { |
| 90 | + |
| 91 | + }, |
| 92 | + |
| 93 | + }; |
| 94 | + |
| 95 | + for (T t : ts) { |
| 96 | + Solution solution; |
| 97 | + |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +int main() { |
| 102 | + testing::InitGoogleTest(); |
| 103 | + |
| 104 | + return RUN_ALL_TESTS(); |
| 105 | +} |
| 106 | + |
| 107 | + |
0 commit comments