Java Solution beats 100% of Submissions
Intuition\n Describe your first thoughts on how to solve this problem. \nThe best way to solve this problem is by recursivly scanning all 8 directions of the ki
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe best way to solve this problem is by recursivly scanning all 8 directions of the king until we have either found a queen or reached the end of the board.\n\n\n\n\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize our chess board\n2. Create a helper function to check a specific direction\n3. Create a boolean helper function to check we are within the board\n4. Check all possible directions\n5. return our ArrayList\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(q) where q is the number of queens on the board\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1) we use the same size board for each scenario\n# Code\n```\nclass Solution {\n    // List to store the positions of queens that can attack the king\n    List<List<Integer>> ans = new ArrayList<>();\n    // 8x8 chess board\n    int[][] board;\n\n    public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {\n        // Initialize the board\n        board = new int[8][8];\n\n        // Place queens on the board\n        for (int[] queen : queens) {\n            board[queen[0]][queen[1]] = 1;\n        }\n\n        // Check all eight possible directions\n        checkDirection(king, -1, 0); // Top\n        checkDirection(king, 1, 0); // Bottom\n        checkDirection(king, 0, -1); // Left\n        checkDirection(king, 0, 1); // Right\n        checkDirection(king, -1, -1); // Top-left\n        checkDirection(king, -1, 1); // Top-right\n        checkDirection(king, 1, -1); // Bottom-left\n        checkDirection(king, 1, 1); // Bottom-right\n\n        return ans;\n    }\n\n    // Helper method to check a specific direction\n    private void checkDirection(int[] king, int rowDir, int colDir) {\n        int row = king[0];\n        int col = king[1];\n\n        while (isValid(row, col)) {\n            row +=rowDir;\n            col += colDir;\n\n            if (isValid(row, col) && board[row][col] == 1) {\n                // Queen found in this direction\n                List<Integer> temp = new ArrayList<>();\n                temp.add(row);\n                temp.add(col);\n                ans.add(temp);\n                return;\n            }\n        }\n    }\n\n    // Helper method to check if a position is within the board limits\n    private boolean isValid(int row, int col) {\n        return row >= 0 && row < 8 && col >= 0 && col < 8;\n    }\n}\n\n```
['Java']