Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Runtime: 1 ms, faster than 98.97% of Java online submissions for Queens That Can Attack the King.
- // Memory Usage: 36.3 MB, less than 100.00% of Java online submissions for Queens That Can Attack the King.
- class Solution {
- public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
- char[][] board = new char[8][8];
- for (int[] queen: queens) board[queen[0]][queen[1]] = 'Q';
- List<List<Integer>> result = new ArrayList<>();
- int[][] directions = { {1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1} };
- for (int[] direction: directions) {
- int r = king[0] + direction[0];
- int c = king[1] + direction[1];
- while (r >= 0 && r < 8 && c >=0 && c < 8) {
- if (board[r][c] == 'Q') {
- result.add(Arrays.asList(r, c));
- break;
- }
- r += direction[0];
- c += direction[1];
- }
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement