Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. /*
  2. Runtime: 1 ms, faster than 98.97% of Java online submissions for Queens That Can Attack the King.
  3. Memory Usage: 36.3 MB, less than 100.00% of Java online submissions for Queens That Can Attack the King.
  4. */
  5.  
  6. class Solution {
  7.     int[][] dirs = new int[][] {
  8.         {1,1},
  9.         {0,1},
  10.         {1,0},
  11.         {1,-1},
  12.         {0,-1},
  13.         {-1,1},
  14.         {-1,0},
  15.         {-1,-1}        
  16.     };
  17.    
  18.     public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
  19.         Set<Short> qSet = new HashSet<>();
  20.         List<List<Integer>> result = new LinkedList<>();
  21.         for (int[] q : queens) {
  22.             qSet.add((short)(q[0] << 4 | q[1]));
  23.         }
  24.        
  25.         for (int[] dir : dirs) {
  26.             int i = king[0];
  27.             int j = king[1];
  28.             while (i >= 0 && j >= 0 &&
  29.                    i < 8 && j < 8) {
  30.                 if (qSet.contains((short)(i << 4 | j))) {
  31.                     result.add(Arrays.asList(i, j));
  32.                     break;
  33.                 }
  34.                 i += dir[0];
  35.                 j += dir[1];
  36.             }
  37.         }
  38.        
  39.         return result;
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement