首页 > 其他分享 >[LeetCode] 2536. Increment Submatrices by One

[LeetCode] 2536. Increment Submatrices by One

时间:2023-01-17 02:55:12浏览次数:49  
标签:row1i matrix int 矩阵 Increment corner query 2536 LeetCode

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 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

子矩阵元素加 1。

给你一个正整数 n ,表示最初有一个 n x n 、下标从 0 开始的整数矩阵 mat ,矩阵中填满了 0 。

另给你一个二维整数数组 query 。针对每个查询 query[i] = [row1i, col1i, row2i, col2i] ,请你执行下述操作:

找出 左上角 为 (row1i, col1i) 且 右下角 为 (row2i, col2i) 的子矩阵,将子矩阵中的 每个元素 加 1 。也就是给所有满足 row1i <= x <= row2i 和 col1i <= y <= col2i 的 mat[x][y] 加 1 。
返回执行完所有操作后得到的矩阵 mat 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/increment-submatrices-by-one
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是二维差分。二维差分可以解决类似这样的问题,比如下图(引用

在一个二维矩阵里,

(a) 如果你想把桔黄色的部分都 + 1,那么就只需要在左上角这个点 + 1,然后遍历整个矩阵,令 grid[i][j] += grid[i][j - 1] 即可

如果你只想让 (b) 图中蓝色的部分都 + 1,那么你需要在图 (c) 的这些位置做加或减的操作,然后再求整个矩阵的前缀和。这道题也是一样的思路,只是注意,需要修改的部分的右下角的坐标不能越界。

时间O(n^2)

空间O(n^2)

Java实现

 1 class Solution {
 2     public int[][] rangeAddQueries(int n, int[][] queries) {
 3         int[][] grid = new int[n][n];
 4         for (int[] q : queries) {
 5             int r1 = q[0];
 6             int c1 = q[1];
 7             int r2 = q[2];
 8             int c2 = q[3];
 9             for (int i = r1; i <= r2; i++) {
10                 grid[i][c1]++;
11                 // 注意不能越界
12                 if (c2 + 1 < n) {
13                     grid[i][c2 + 1]--;
14                 }
15             }
16         }
17 
18         for (int i = 0; i < n; i++) {
19             for (int j = 1; j < n; j++) {
20                 grid[i][j] += grid[i][j - 1];
21             }
22         }
23         return grid;
24     }
25 }

 

LeetCode 题目总结

标签:row1i,matrix,int,矩阵,Increment,corner,query,2536,LeetCode
From: https://www.cnblogs.com/cnoodle/p/17056832.html

相关文章

  • [LeetCode] 1328. Break a Palindrome 破坏回文串
    GivenapalindromicstringoflowercaseEnglishletterspalindrome,replaceexactlyonecharacterwithanylowercaseEnglishlettersothattheresultingstri......
  • [LeetCode] 1814. Count Nice Pairs in an Array
    Youaregivenanarray nums thatconsistsofnon-negativeintegers.Letusdefine rev(x) asthereverseofthenon-negativeinteger x.Forexample, rev(1......
  • 【LeetCode链表#8】翻转链表(双指针+递归)
    翻转链表力扣题目链接(opensnewwindow)题意:反转一个单链表。示例:输入:1->2->3->4->5->NULL输出:5->4->3->2->1->NULL思路分析双指针法是本体的最基本的解法,由......
  • LeetCode.242 有效的字母异位词
    1.题目:给定两个字符串s和t,编写一个函数来判断t是否是s的字母异位词。注意:若 s和t 中每个字符出现的次数都相同,则称 s和t 互为字母异位词。 2.代码实现:方法......
  • 【拓扑排序】LeetCode 207. 课程表
    题目链接207.课程表思路参考Krahets大佬的思路代码classSolution{publicbooleancanFinish(intnumCourses,int[][]prerequisites){int[]inde......
  • 代码随想录算法训练营day04 | leetcode
    基础知识记录一下栈实现及操作publicclassArrayDequeStack{publicvoidmain(){ArrayDequestack=newArrayDeque();//大小......
  • LeetCode.142 环形链表II
    1.题目:给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。如果链表中有某个节点,可以通过连续跟踪next指针再次到达,则链表中存在......
  • [LeetCode] 1813. Sentence Similarity III
    Asentenceisalistofwordsthatareseparatedbyasinglespacewithnoleadingortrailingspaces.Forexample, "HelloWorld", "HELLO", "helloworldhel......
  • 【BFS】LeetCode 417. 太平洋大西洋水流问题
    题目链接417.太平洋大西洋水流问题思路问题可以转换成从四个边界出发,能和内部哪些点连通。因为涉及到两个可达性问题,所以用两个boolean类型矩阵分别记录一个点到太......
  • 【LeetCode链表#7】设计一个链表并实现常见的操作方法
    设计链表题目力扣题目链接设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val和next。val是当前节点的值,next是指向下一个节点的指......