class Solution { public: vector<vector<int>> dirs={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}}; void gameOfLife(vector<vector<int>>& board) { //time O(2*9*m*n) int r=board.size(); if(r==0) return; int c=board[0].size(); if(c==0) return; vector<vector<int>> cur(r,vector(c,0)); for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ for(auto dir:dirs){ int x=i+dir[0],y=j+dir[1]; if(x<0 || x>=r || y<0 || y>=c) continue; cur[i][j]+=board[x][y]; } } } for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ if( cur[i][j]==3 || board[i][j]==1 && cur[i][j]==2 ) board[i][j]=1; else board[i][j]=0; } } } };