FastPrepFastPrep
Problem Brief

Board Coloring and Query Processing

OA

Consider a rectangular h × w board with all cells initially white. You are to process several queries of the following types:

  • "x a b" - color the white cell (a, b) (0-based coordinates, the first one is a row index, and the second one is a column index) black;
  • "> a b" - find the leftmost white cell to the right of the white cell (a, b);
  • "< a b" - find the rightmost white cell to the left of the white cell (a, b);
  • "v a b" - the same, but the search should be done downwards;
  • "^ a b" - the same, but the search should be done upwards;
  • For each query, except the ones of the first type, find the answer.

    For each query except the ones of the first type, store the answer's coordinates in the array. If the desired cell doesn't exist, store [-1, -1] instead. The answers should be stored in the same order as the queries.

    1Example 1

    Input
    h = 3, w = 5, queries = ["v 1 2", "x 2 2", "v 1 2", "> 2 1", "x 2 3", "> 2 1", "< 2 0"]
    Output
    [[2, 2], [-1, -1], [2, 3], [2, 4], [-1, -1]]
    Explanation
    :3

    Constraints

    Limits and guarantees your solution can rely on.

    • 1 ≤ h ≤ 500.
    • 1 ≤ w ≤ 500.
    • 5 ≤ queries.length ≤ 10^4 (OR MAYBE 104).
    public int[][] solution(int h, int w, String[] queries) {
      // write your code here
    }
    
    Input

    h

    3

    w

    5

    queries

    ["v 1 2", "x 2 2", "v 1 2", "> 2 1", "x 2 3", "> 2 1", "< 2 0"]

    Output

    [[2, 2], [-1, -1], [2, 3], [2, 4], [-1, -1]]

    Sign in to submit your solution.