Python faster than 100%
The comments in the code are self explanatory. Here\'s a brief overview:\n1) Build a grid of size 8x8\n2) Populate the grid with queens\n3) Traverse in all 8 di
The comments in the code are self explanatory. Here\'s a brief overview:\n1) Build a grid of size 8x8\n2) Populate the grid with queens\n3) Traverse in all 8 directions, starting from the King\'s coordinates. When you encounter a Queen, update the count and don\'t go any further.\n\n\t\tclass Solution:\n\t\t\tdef queensAttacktheKing(self, queens: List[List[int]], king: List[int]) -> List[List[int]]:\n\t\t\t\t# build the grid\n\t\t\t\tgrid=[[None for c in range(8)] for r in range(8)]\n\t\t\t\tfor queen in queens:\n\t\t\t\t\tgrid[queen[0]][queen[1]]="Q"\n\t\t\t\tans=[]\n\t\t\t\trow,col=king\n\t\t\t\t# go up\n\t\t\t\tfor rowup in reversed(range(row)):\n\t\t\t\t\tif grid[rowup][col]=="Q":\n\t\t\t\t\t\tans.append([rowup,col])\n\t\t\t\t\t\tbreak\n\t\t\t\t# go down\n\t\t\t\tfor rowdown in range(row+1,8):\n\t\t\t\t\tif grid[rowdown][col]=="Q":\n\t\t\t\t\t\tans.append([rowdown,col])\n\t\t\t\t\t\tbreak\n\t\t\t\t# go left\n\t\t\t\tfor colleft in reversed(range(col)):\n\t\t\t\t\tif grid[row][colleft]=="Q":\n\t\t\t\t\t\tans.append([row,colleft])\n\t\t\t\t\t\tbreak\n\t\t\t\t# go right\n\t\t\t\tfor colright in range(col+1,8):\n\t\t\t\t\tif grid[row][colright]=="Q":\n\t\t\t\t\t\tans.append([row,colright])\n\t\t\t\t\t\tbreak\n\t\t\t\t# go diagonally up leftside\n\t\t\t\trowx,colx=king\n\t\t\t\twhile rowx>0 and colx>0:\n\t\t\t\t\trowx-=1\n\t\t\t\t\tcolx-=1\n\t\t\t\t\tif grid[rowx][colx]=="Q":\n\t\t\t\t\t\tans.append([rowx,colx])\n\t\t\t\t\t\tbreak\n\t\t\t\t# go diagonally down rightside\n\t\t\t\trowx,colx=king\n\t\t\t\twhile rowx<7 and colx<7:\n\t\t\t\t\trowx+=1\n\t\t\t\t\tcolx+=1\n\t\t\t\t\tif grid[rowx][colx]=="Q":\n\t\t\t\t\t\tans.append([rowx,colx])\n\t\t\t\t\t\tbreak\n\t\t\t\t# go diagonally up rightside\n\t\t\t\trowx,colx=king\n\t\t\t\twhile rowx>0 and colx<7:\n\t\t\t\t\trowx-=1\n\t\t\t\t\tcolx+=1\n\t\t\t\t\tif grid[rowx][colx]=="Q":\n\t\t\t\t\t\tans.append([rowx,colx])\n\t\t\t\t\t\tbreak\n\t\t\t\t# go diagonally down leftside\n\t\t\t\trowx,colx=king\n\t\t\t\twhile rowx<7 and colx>0:\n\t\t\t\t\trowx+=1\n\t\t\t\t\tcolx-=1\n\t\t\t\t\tif grid[rowx][colx]=="Q":\n\t\t\t\t\t\tans.append([rowx,colx])\n\t\t\t\t\t\tbreak\n\t\t\t\treturn ans
[]