Queen's Attack II Discussions | Algorithms | HackerRank

Sort by

recency

|

1016 Discussions

|

  • + 0 comments

    Given plenty of time, I can complete medium diffcult level problem. but I cannot complate in half an hour.

  • + 0 comments

    If you’re looking for a great investment or a new home, there are some interesting options available. I recently came across an apartment for sale in Thessaloniki that caught my attention due to its central location and modern amenities. The neighborhood is vibrant, with easy access to shops, cafes, and public transport. Pricing seems reasonable compared to similar properties in the area. I’m curious if anyone has experience buying in Thessaloniki and can share tips on the process or reliable real estate agents. Any advice would be really appreciated!

  • + 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;
        }
    }
    

    }