LeetCode //C - 363. Max Sum of Rectangle No Larger Than K

363. Max Sum of Rectangle No Larger Than K

Given an m x n matrix matrix and an integer k, return the max sum of a rectangle in the matrix such that its sum is no larger than k.

It is guaranteed that there will be a rectangle with a sum no larger than k.

Example 1:

Input: matrix = \[1,0,1,0,-2,3], k = 2
Output: 2
Explanation: Because the sum of the blue rectangle \[0, 1, -2, 3] is 2, and 2 is the max number no larger than k (k = 2).

Example 2:

Input: matrix = \[2,2,-1], k = 3
Output: 3

Constraints:
  • m == matrix.length
  • n == matrixi.length
  • 1 <= m, n <= 100
  • -100 <= matrixij <= 100
  • − 1 0 5 < = k < = 1 0 5 -10^5 <= k <= 10^5 −105<=k<=105

From: LeetCode

Link: 363. Max Sum of Rectangle No Larger Than K


Solution:

Ideas:
  • Outer loops: We loop over all pairs of starting and ending rows.
  • Column sum array: We calculate the cumulative sums for columns between the two rows, effectively reducing the 2D matrix to a 1D array problem.
  • Brute-force subarray sum check: We calculate all possible sums of subarrays in the 1D colSums array and track the largest one that is no larger than k.
Code:
c 复制代码
int maxSumSubmatrix(int** matrix, int matrixSize, int* matrixColSize, int k) {
    int maxSum = INT_MIN;
    int rows = matrixSize, cols = *matrixColSize;

    // Loop through the possible row start points
    for (int startRow = 0; startRow < rows; ++startRow) {
        // Temporary array to store column sums
        int* colSums = (int*)calloc(cols, sizeof(int));
        
        // Loop through the possible row end points
        for (int endRow = startRow; endRow < rows; ++endRow) {
            // Update column sums
            for (int col = 0; col < cols; ++col) {
                colSums[col] += matrix[endRow][col];
            }

            // Now we need to find the subarray no larger than k in the colSums array
            // Brute-force approach for subarray sums
            for (int startCol = 0; startCol < cols; ++startCol) {
                int currentSum = 0;
                for (int endCol = startCol; endCol < cols; ++endCol) {
                    currentSum += colSums[endCol];
                    if (currentSum <= k) {
                        if (currentSum > maxSum) {
                            maxSum = currentSum;
                        }
                    }
                }
            }
        }
        free(colSums);
    }

    return maxSum;
}
相关推荐
Lyyaoo.12 小时前
【回溯】【中等】全排列
java·数据结构·算法
写后端的胖头鱼12 小时前
一文讲懂JVM与调优
jvm·后端·算法·架构·jvm调优
水饺编程12 小时前
第5章,[Win32 章节] :绘制平面直角坐标系
c语言·c++·windows·visual studio
土司大王12 小时前
LeetCode 79 单词搜索:Java 回溯模板、网格 DFS 与剪枝优化
java·算法·leetcode·深度优先
ECT-OS-JiuHuaShan12 小时前
哲学是迭代学,数学是拓扑学
开发语言·人工智能·学习·算法·机器学习·php·拓扑学
All for pursuit.12 小时前
【数组-5】560.和为K的子数组
数据结构·c++·算法·leetcode
白色的北极熊12 小时前
c 语言 输出矩形
c语言
Beyond_System|系统之外12 小时前
【学编程】Python基础编程题100道(1-20)
java·数据结构·算法
维克兜率天12 小时前
【维克】一个极端值,毁掉了整个因子
大数据·人工智能·python·算法·机器学习
船厂电气自动化ai大模型13 小时前
AI大模型与数学 第75课 基、维度、向量空间坐标
数据结构·线性代数·算法·机器学习·推荐算法