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.
Queen's Attack II
Queen's Attack II
Sort by
recency
|
1002 Discussions
|
Please Login in order to post a comment
**simple python solution **
def queensAttack(n, k, r_q, c_q, obstacles):
My Java solution with linear time and o(k) space complexity:
int queensAttack(int n, int k, int r_q, int c_q, vector> obstacles) {
}
Hint 1- Just check all obstacles that they are on one of four lines. Then you will have at maximun eight obstacles. Hint 2 - then just calculate distance to obstacle or edge of the board.
public class Solution {
// Complete the queensAttack function below. static int queensAttack(int n, int k, int r, int c, int[][] obstacles) { HashMap> cache = new HashMap<>(); for (int i = 0; i < obstacles.length; i++) { if (cache.containsKey(obstacles[i][0])) { cache.get(obstacles[i][0]).add(obstacles[i][1]); } else { cache.put(obstacles[i][0], new HashSet()); cache.get(obstacles[i][0]).add(obstacles[i][1]); } } int counter = 0; // right for (int i = c + 1; i <= n; i++) { if (cache.containsKey(r) && cache.get(r).contains(i)) { break; } counter++; }
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
} }