You are viewing a single comment's thread. Return to all comments →
Respectfully, this problem is not "medium", it's freaking hard! (ಥ﹏ಥ)
Heres is my c++ solutions, hope is helps someone.
struct Plus { int area; vector<pair<int,int>> cells; }; vector<Plus> allPlussesAt(int y, int x, const vector<string>& grid) { vector<Plus> result; if (grid[y][x] != 'G') return result; int arm = 0; int rows = grid.size(), cols = grid[0].size(); while (y-arm >= 0 && y+arm < rows && x-arm >= 0 && x+arm < cols && grid[y-arm][x] == 'G' && grid[y+arm][x] == 'G' && grid[y][x-arm] == 'G' && grid[y][x+arm] == 'G') { vector<pair<int,int>> cells; cells.push_back({y,x}); for (int k=1; k<=arm; k++) { cells.push_back({y-k,x}); cells.push_back({y+k,x}); cells.push_back({y,x-k}); cells.push_back({y,x+k}); } result.push_back({1 + 4*arm, cells}); arm++; } return result; } vector<Plus> allPlusses(const vector<string>& grid) { vector<Plus> plusses; for (int y=0; y<grid.size(); y++) { for (int x=0; x<grid[0].size(); x++) { auto p = allPlussesAt(y, x, grid); plusses.insert(plusses.end(), p.begin(), p.end()); } } return plusses; } bool overlap(const Plus& a, const Plus& b) { for (auto &cellA : a.cells) { for (auto &cellB : b.cells) { if (cellA == cellB) return true; } } return false; } int maxPlusAreaAt(int y, int x, const vector<string>& grid) { if (grid[y][x] != 'G') return 0; int arm = 0; int rows = grid.size(), cols = grid[0].size(); while (y-arm >= 0 && y+arm < rows && x-arm >= 0 && x+arm < cols && grid[y-arm][x] == 'G' && grid[y+arm][x] == 'G' && grid[y][x-arm] == 'G' && grid[y][x+arm] == 'G') { arm++; } arm--; return 1 + 4*arm; } int twoPluses(vector<string> grid) { vector<Plus> plusses = allPlusses(grid); int best = 0; for (int i=0; i<plusses.size(); i++) { for (int j=i+1; j<plusses.size(); j++) { if (!overlap(plusses[i], plusses[j])) { best = max(best, plusses[i].area * plusses[j].area); } } } return best; }
Seems like cookies are disabled on this browser, please enable them to open this website
Ema's Supercomputer
You are viewing a single comment's thread. Return to all comments →
Respectfully, this problem is not "medium", it's freaking hard! (ಥ﹏ಥ)
Heres is my c++ solutions, hope is helps someone.