Queen's Attack II Discussions | Algorithms | HackerRank
  • + 0 comments

    Hi, a solution in JS: algorithm: 1. find all the squares the queen can attack initially (total variable in code) 2. iterate through the obstacles array, and for each obstacle, i first check if it is blocking the queen from attacking by veryfing of any obstacle is in one of the 8 paths the queen can attack, if so i calculate how many squares this obstacles block (block variable in code) and then i substract this value from the total. 3. pay attention that there might be more than 1 obstacle in a certain direction/path, so you want to make sure you dont substract more than you need(thats why i have the max's variables)

    function queensAttack(n, k, r_q, c_q, obstacles) { let left = c_q - 1; let right = n - c_q; let up = n - r_q; let down = r_q - 1; let leftDown = Math.min(left, down); let leftUp = Math.min(left, up); let rightDown = Math.min(right, down); let rightUp = Math.min(right, up); let total = left + right + up + down + leftUp + leftDown + rightUp + rightDown; let maxL = 0, maxR = 0, maxU = 0,maxD = 0, maxLU = 0, maxLD = 0, maxRD = 0, maxRU = 0; let r, c, block, temp = 0; for(let i = 0; i < k; i++) {

        r = obstacles[i][0];
        c = obstacles[i][1];
        if(r_q === r && c >= 1 && c < c_q){ //left
            block = c;
            if(block > maxL){
                temp = block - maxL;
                maxL = block;
                total -= temp;
            }
        } 
        else if(r_q === r && c > c_q && c <= n){ //right
            block = (n-c) + 1
            if(block > maxR){
                temp = block - maxR ;
                maxR = block;
                total -= temp;
            }
        }  
    
        else if(c_q === c && r_q < r && r <= n){ //up
            block = (n-r) + 1; 
            if(block > maxU){
                temp = block - maxU;
                maxU = block;
                total -= temp;
            }
        }
        else if(c_q === c && r >= 1 && r < r_q){ //down
            block = r;
            if(block > maxD){
                temp = block - maxD;
                maxD = block;
                total -= temp;
            }
        } 
        else if(r > r_q && c_q > c && (r - r_q) === (c_q - c)){ //leftUp
            block = Math.min(c, (n-r)+1); 
            if(block > maxLU){
                temp = block - maxLU;
                maxLU = block;
                total -= temp;
            }
        }
        else if(r_q > r && c_q > c && (c_q - c) === (r_q - r)){ //leftDown
            block = Math.min(c, r);
            if(block > maxLD){
                temp = block - maxLD;
                maxLD = block;
                total -= temp;
            }
        }
        else if(r > r_q && c > c_q && (r - r_q) === (c - c_q)){ //rightUp
            block = Math.min((n-c)+1, (n-r)+1);
            if(block > maxRU){
                temp = block - maxRU;
                maxRU = block;
                total -= temp;
            }
        }
        else if(r_q > r && c > c_q &&(r_q - r) === (c - c_q)){ //rightDown
            block = Math.min((n-c)+1, r);
            console.log("RD Block", block);
            if(block > maxRD){
                temp = block - maxRD;
                maxRD = block;
                total -= temp;
            }
        }
    }
    return total;
    

    }