Блог пользователя Loserinlife

Автор Loserinlife, история, 7 месяцев назад, По-английски

Given a n * m matrix of 0s and 1s. For each square,find the sum of Manhattan distances from that square to the first kth nearest 1s.

(Distance to closest 1 + distance to second closest 1 + ...)

n, m <= 2000

k <= n * m

There are at least k ones in the matrix. Thanks!

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
7 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
  • »
    »
    7 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I think it's a bit different because I want the sum of the first kth distances, not only the kth.

    • »
      »
      »
      7 месяцев назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      the idea is a bit similar, in order to calculate the sum of the first kth distance, we need to consider 4 cases to handle |x1 — x2| + |y1 — y2|.

      (x1 > x2, y1 > y2)

      (x1 < x2, y1 < y2)

      (x1 > x2, y1 < y2)

      (x1 < x2, y1 > y2)

      we can see that we can divide the square into 4 seperate smaller squares to handle that.

»
7 месяцев назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Can you provide the source of this task? I think I have something quite close to solving the task but I need some research on it