Problem · Matrix

Board Coloring and Query Processing

Learn this problem
MediumPayPayOA

Problem statement

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.

    Function

    solution(h: int, w: int, queries: String[]) → int[][]

    Examples

    Example 1

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

    Constraints

    • 1 ≤ h ≤ 500.
    • 1 ≤ w ≤ 500.
    • 5 ≤ queries.length ≤ 10^4 (OR MAYBE 104).

    More PayPay problems

    drafts saved locally
    public int[][] solution(int h, int w, String[] queries) {
      // write your code here
    }
    
    h3
    w5
    queries["v 1 2", "x 2 2", "v 1 2", "> 2 1", "x 2 3", "> 2 1", "< 2 0"]
    expected[[2, 2], [-1, -1], [2, 3], [2, 4], [-1, -1]]
    checking account