Queen's Attack II Discussions | Algorithms | HackerRank
We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
classResult{/* * Complete the 'queensAttack' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. INTEGER n * 2. INTEGER k * 3. INTEGER r_q * 4. INTEGER c_q * 5. 2D_INTEGER_ARRAY obstacles */staticintcellConsume=0;publicstaticintqueensAttack(intn,intk,intr_q,intc_q,List<List<Integer>>obstacles){// Write your code here// possible path for a queen is 8 directions,HashMap<Integer,List<Integer>>obstaclesMap=convertObstaclesIntoMap(obstacles);bottomIteration(r_q,c_q,obstaclesMap);southWestIteration(r_q,c_q,obstaclesMap);leftIteration(r_q,c_q,obstaclesMap);northWestIteration(r_q,c_q,n,obstaclesMap);topIteration(r_q,c_q,n,obstaclesMap);northEastIteration(r_q,c_q,n,obstaclesMap);rightIteration(r_q,c_q,n,obstaclesMap);southEastIteration(r_q,c_q,n,obstaclesMap);returncellConsume;}privatestaticHashMap<Integer,List<Integer>>convertObstaclesIntoMap(List<List<Integer>>obstacles){HashMap<Integer,List<Integer>>obstaclesMap=newHashMap<>();obstacles.forEach(x->{obstaclesMap.compute(x.get(0),(k,existingList)->{if(existingList==null){// Key not present, create a new listreturnnewArrayList<>(Collections.singletonList(x.get(1)));}else{// Key already exists, add value to the existing listexistingList.add(x.get(1));returnexistingList;}});});returnobstaclesMap;}publicstaticvoidleftIteration(intr_q,intc_q,HashMap<Integer,List<Integer>>obstacles){for(intr=r_q,c=c_q-1;c>=1;c--){if(obstacles.get(r)!=null&&obstacles.get(r).contains(c)){return;}cellConsume++;}}publicstaticvoidbottomIteration(intr_q,intc_q,Map<Integer,List<Integer>>obstacles){for(intr=r_q-1,c=c_q;r>=1;r--){if(obstacles.get(r)!=null&&obstacles.get(r).contains(c)){return;}cellConsume++;}}publicstaticvoidsouthWestIteration(intr_q,intc_q,HashMap<Integer,List<Integer>>obstacles){for(intr=r_q-1,c=c_q-1;r>=1&c>=1;r--,c--){if(obstacles.get(r)!=null&&obstacles.get(r).contains(c)){return;}cellConsume++;}}publicstaticvoidnorthWestIteration(intr_q,intc_q,intn,HashMap<Integer,List<Integer>>obstacles){for(intr=r_q+1,c=c_q-1;r<=n&c>=1;r++,c--){if(obstacles.get(r)!=null&&obstacles.get(r).contains(c)){return;}cellConsume++;}}publicstaticvoidtopIteration(intr_q,intc_q,intn,HashMap<Integer,List<Integer>>obstacles){for(intr=r_q+1,c=c_q;r<=n;r++){if(obstacles.get(r)!=null&&obstacles.get(r).contains(c)){return;}cellConsume++;}}publicstaticvoidnorthEastIteration(intr_q,intc_q,intn,HashMap<Integer,List<Integer>>obstacles){for(intr=r_q+1,c=c_q+1;r<=n&c<=n;r++,c++){if(obstacles.get(r)!=null&&obstacles.get(r).contains(c)){return;}cellConsume++;}}publicstaticvoidrightIteration(intr_q,intc_q,intn,HashMap<Integer,List<Integer>>obstacles){for(intr=r_q,c=c_q+1;c<=n;c++){if(obstacles.get(r)!=null&&obstacles.get(r).contains(c)){return;}cellConsume++;}}publicstaticvoidsouthEastIteration(intr_q,intc_q,intn,HashMap<Integer,List<Integer>>obstacles){for(intr=r_q-1,c=c_q+1;r>=1&c<=n;r--,c++){if(obstacles.get(r)!=null&&obstacles.get(r).contains(c)){return;}cellConsume++;}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Queen's Attack II
You are viewing a single comment's thread. Return to all comments →
Tried to make the code setp by step