Queen's Attack II Discussions | Algorithms | HackerRank

Sort by

recency

|

1014 Discussions

|

  • + 0 comments

    I'm failing in a case that maybe I didn't consider. I want to see the discussion to see if someone else had a similar issue or to have ideas of the possible scenarios, instead, I'm getting the Détente Musculaire result. Please avoid pasting here your code. We don't need it, nobody need it!

  • + 0 comments

    Here's my solution in TypeScript:

    (Notice that the arguments for the queen's position are wrong in the original function.)

    type Obstacles = {
      l: number;
      r: number;
      t: number;
      b: number;
      tr: number;
      tl: number;
      br: number;
      bl: number;
    };
    
    function queensAttack(n: number, _k: number, c_q: number, r_q: number, obstacles: number[][]): number {
      const bounds = {
        left: r_q - 1,
        top: n - c_q,
        right: n - r_q,
        bottom: c_q - 1,
      }
      
      const limits = obstacles.reduce<Obstacles>((dict, [coordY, coordX]) => {
        const dX = coordX - r_q;
        const dY = coordY - c_q;
        
        if(dY === 0) {
          if(dX > 0) {
            dict.r = Math.min(dX - 1, dict.r);
          } else {
            dict.l = Math.min(-dX - 1, dict.l);
          }
        } else if (dX === 0) {
          if(dY > 0) {
            dict.t = Math.min(dY - 1, dict.t);
          } else {
            dict.b = Math.min(-dY - 1, dict.b);
          }
        } else if (Math.abs(dX) === Math.abs(dY)) {
          if(dY > 0) {
            if(dX > 0) {
              dict.tr = Math.min(dX - 1, dict.tr);
            } else {
              dict.tl = Math.min(-dX - 1, dict.tl);
            }
          } else {
            if(dX > 0){
              dict.br = Math.min(dX - 1, dict.br);
            } else {
              dict.bl = Math.min(-dX - 1, dict.bl);
            }
          }
        }
        return dict;
      }, {
        t: bounds.top,
        b: bounds.bottom,
        l: bounds.left,
        r: bounds.right,
        tr: Math.min(bounds.top, bounds.right),
        tl: Math.min(bounds.top, bounds.left),
        bl: Math.min(bounds.bottom, bounds.left),
        br: Math.min(bounds.bottom, bounds.right),
      });
      
      return Object.values(limits).reduce((acc, curr) => acc + curr, 0);
    }
    
  • + 0 comments

    import java.util.*; import java.awt.Point;

    //thank me later public class Solution { public static void main(String[] args){ Scanner in = new Scanner(System.in);

        int n = in.nextInt();
        int k = in.nextInt();
    
        Point queen = new Point(in.nextInt(), in.nextInt());
    
        HashSet<Point> obstacles = new HashSet<Point>();
    
        for(int i = 0; i < k; i++){
            obstacles.add(new Point(in.nextInt(), in.nextInt()));
        }
    
        Stack<Direction> directions = getAllDirections();
        int possiblePoints = 0;
    
        while(!directions.empty()){
            Direction d = directions.pop();
            Point cur = new Point(queen);
            cur.translate(d.x, d.y);
    
            while(isInside(cur, n) && !obstacles.contains(cur)){
                possiblePoints++;
                cur.translate(d.x, d.y);
            }
        }
    
        System.out.println(possiblePoints);
    }
    
    private static boolean isInside(Point p, int n){
        return p.x > 0 && p.y > 0 && p.x <= n && p.y <= n;
    }
    
    private static Stack<Direction> getAllDirections(){
        Stack<Direction> directions = new Stack<Direction>();
        directions.push(new Direction(0, 1));
        directions.push(new Direction(1, 1));
        directions.push(new Direction(1, 0));
        directions.push(new Direction(1, -1));
        directions.push(new Direction(0, -1));
        directions.push(new Direction(-1, -1));
        directions.push(new Direction(-1, 0));
        directions.push(new Direction(-1, 1));
        return directions;
    }
    
    private static class Direction {
        int x, y;
        public Direction(int x, int y){
            this.x = x;
            this.y = y;
        }
    }
    

    }

  • + 1 comment

    Cries in no. 1 grid problem hater:

    int queensAttack(int n, int k, int r_q, int c_q, vector<vector<int>> obstacles) {
        set<pair<int,int>> obs;
        for (auto &o : obstacles) {
            obs.insert({o[0], o[1]});
        }
    
        vector<pair<int,int>> directions = {
            {1, 0}, {-1, 0}, {0, 1}, {0, -1},
            {1, 1}, {1, -1}, {-1, 1}, {-1, -1}
        };
    
        int count = 0;
    
        for (auto [dr, dc] : directions) {
            int r = r_q + dr;
            int c = c_q + dc;
    
            while (r >= 1 && r <= n && c >= 1 && c <= n && !obs.count({r,c})) {
                count++;
                r += dr;
                c += dc;
            }
        }
    
        return count;
    }
    
  • + 0 comments

    step 1: calculate the maximum attacks in eight directions; step 2: if there is obstacle in any one of the eight direction, which one smaller is the distance. def queensAttack(n, k, r_q, c_q, obstacles): # Write your code here dir_dis = {(0,1):n-c_q, (0,-1):c_q - 1, (1, 0): n-r_q, (-1, 0): r_q - 1, (-1, -1):min(r_q - 1, c_q - 1), (1, 1):min(n-r_q, n-c_q), (1, -1): min(n-r_q, c_q-1), (-1, 1):min(r_q-1, n-c_q) } for ro, co in obstacles: dx = ro - r_q dy = co - c_q if dx == 0 or dy == 0 or abs(dx) == abs(dy): dis = abs(dx) if abs(dx) == abs(dy) else max(abs(dx), abs(dy)) di = (dx/dis, dy/dis) dir_dis[di] = min(dir_dis[di], dis - 1) return sum(dir_dis.values())