返回介绍

solution / 2500-2599 / 2536.Increment Submatrices by One / README_EN

发布于 2024-06-17 01:03:04 字数 6139 浏览 0 评论 0 收藏 0

2536. Increment Submatrices by One

中文文档

Description

You are given a positive integer n, indicating that we initially have an n x n 0-indexed integer matrix mat filled with zeroes.

You are also given a 2D integer array query. For each query[i] = [row1i, col1i, row2i, col2i], you should do the following operation:

  • Add 1 to every element in the submatrix with the top left corner (row1i, col1i) and the bottom right corner (row2i, col2i). That is, add 1 to mat[x][y] for all row1i <= x <= row2i and col1i <= y <= col2i.

Return_ the matrix_ mat_ after performing every query._

 

Example 1:

Input: n = 3, queries = [[1,1,2,2],[0,0,1,1]]
Output: [[1,1,0],[1,2,1],[0,1,1]]
Explanation: The diagram above shows the initial matrix, the matrix after the first query, and the matrix after the second query.
- In the first query, we add 1 to every element in the submatrix with the top left corner (1, 1) and bottom right corner (2, 2).
- In the second query, we add 1 to every element in the submatrix with the top left corner (0, 0) and bottom right corner (1, 1).

Example 2:

Input: n = 2, queries = [[0,0,1,1]]
Output: [[1,1],[1,1]]
Explanation: The diagram above shows the initial matrix and the matrix after the first query.
- In the first query we add 1 to every element in the matrix.

 

Constraints:

  • 1 <= n <= 500
  • 1 <= queries.length <= 104
  • 0 <= row1i <= row2i < n
  • 0 <= col1i <= col2i < n

Solutions

Solution 1

class Solution:
  def rangeAddQueries(self, n: int, queries: List[List[int]]) -> List[List[int]]:
    mat = [[0] * n for _ in range(n)]
    for x1, y1, x2, y2 in queries:
      mat[x1][y1] += 1
      if x2 + 1 < n:
        mat[x2 + 1][y1] -= 1
      if y2 + 1 < n:
        mat[x1][y2 + 1] -= 1
      if x2 + 1 < n and y2 + 1 < n:
        mat[x2 + 1][y2 + 1] += 1

    for i in range(n):
      for j in range(n):
        if i:
          mat[i][j] += mat[i - 1][j]
        if j:
          mat[i][j] += mat[i][j - 1]
        if i and j:
          mat[i][j] -= mat[i - 1][j - 1]
    return mat
class Solution {
  public int[][] rangeAddQueries(int n, int[][] queries) {
    int[][] mat = new int[n][n];
    for (var q : queries) {
      int x1 = q[0], y1 = q[1], x2 = q[2], y2 = q[3];
      mat[x1][y1]++;
      if (x2 + 1 < n) {
        mat[x2 + 1][y1]--;
      }
      if (y2 + 1 < n) {
        mat[x1][y2 + 1]--;
      }
      if (x2 + 1 < n && y2 + 1 < n) {
        mat[x2 + 1][y2 + 1]++;
      }
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (i > 0) {
          mat[i][j] += mat[i - 1][j];
        }
        if (j > 0) {
          mat[i][j] += mat[i][j - 1];
        }
        if (i > 0 && j > 0) {
          mat[i][j] -= mat[i - 1][j - 1];
        }
      }
    }
    return mat;
  }
}
class Solution {
public:
  vector<vector<int>> rangeAddQueries(int n, vector<vector<int>>& queries) {
    vector<vector<int>> mat(n, vector<int>(n));
    for (auto& q : queries) {
      int x1 = q[0], y1 = q[1], x2 = q[2], y2 = q[3];
      mat[x1][y1]++;
      if (x2 + 1 < n) {
        mat[x2 + 1][y1]--;
      }
      if (y2 + 1 < n) {
        mat[x1][y2 + 1]--;
      }
      if (x2 + 1 < n && y2 + 1 < n) {
        mat[x2 + 1][y2 + 1]++;
      }
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (i > 0) {
          mat[i][j] += mat[i - 1][j];
        }
        if (j > 0) {
          mat[i][j] += mat[i][j - 1];
        }
        if (i > 0 && j > 0) {
          mat[i][j] -= mat[i - 1][j - 1];
        }
      }
    }
    return mat;
  }
};
func rangeAddQueries(n int, queries [][]int) [][]int {
  mat := make([][]int, n)
  for i := range mat {
    mat[i] = make([]int, n)
  }
  for _, q := range queries {
    x1, y1, x2, y2 := q[0], q[1], q[2], q[3]
    mat[x1][y1]++
    if x2+1 < n {
      mat[x2+1][y1]--
    }
    if y2+1 < n {
      mat[x1][y2+1]--
    }
    if x2+1 < n && y2+1 < n {
      mat[x2+1][y2+1]++
    }
  }
  for i := 0; i < n; i++ {
    for j := 0; j < n; j++ {
      if i > 0 {
        mat[i][j] += mat[i-1][j]
      }
      if j > 0 {
        mat[i][j] += mat[i][j-1]
      }
      if i > 0 && j > 0 {
        mat[i][j] -= mat[i-1][j-1]
      }
    }
  }
  return mat
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文