Advertisement
nikunjsoni

361

Jun 1st, 2021
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int maxKilledEnemies(vector<vector<char>>& grid) {
  4.         int rows = grid.size(), cols = grid[0].size();
  5.         int ans = 0, rowCount=0;
  6.         vector<int> colCount(cols, 0);
  7.        
  8.         for(int row=0; row<rows; row++){
  9.             for(int col=0; col<cols; col++){
  10.                 if(col == 0 || grid[row][col-1] == 'W'){
  11.                     rowCount = 0;
  12.                     for(int k=col; k<cols; k++){
  13.                         if(grid[row][k] == 'W')
  14.                             break;
  15.                         else if(grid[row][k] == 'E')
  16.                             rowCount += 1;
  17.                     }
  18.                 }
  19.                 if(row == 0 || grid[row-1][col] == 'W'){
  20.                     colCount[col] = 0;
  21.                     for(int k=row; k<rows; k++){
  22.                         if(grid[k][col] == 'W')
  23.                             break;
  24.                         else if(grid[k][col] == 'E')
  25.                             colCount[col] += 1;
  26.                     }
  27.                 }
  28.                 if(grid[row][col] == '0')
  29.                     ans = max(ans, rowCount+colCount[col]);
  30.             }
  31.         }
  32.         return ans;
  33.     }
  34. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement