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 == matrix[i].length
  • 1 <= m, n <= 100
  • -100 <= matrix[i][j] <= 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;
}
相关推荐
我真不会起名字啊26 分钟前
C、C++中的sprintf和stringstream的使用
java·c语言·c++
资深web全栈开发39 分钟前
LeetCode 3625. 统计梯形的数目 II
算法·leetcode·组合数学
橘颂TA40 分钟前
【剑斩OFFER】算法的暴力美学——外观数列
算法·leetcode·职场和发展·结构与算法
Liangwei Lin41 分钟前
洛谷 P1434 [SHOI2002] 滑雪
算法
c#上位机1 小时前
halcon图像增强之自动灰度拉伸
图像处理·算法·c#·halcon·图像增强
rit84324991 小时前
压缩感知信号恢复算法:OMP与CoSaMP对比分析
数据库·人工智能·算法
Pluchon2 小时前
硅基计划4.0 算法 FloodFill算法
java·算法·leetcode·决策树·逻辑回归·深度优先·图搜索算法
菜鸟233号2 小时前
力扣347. 前k个高频元素 java实现
算法
剪一朵云爱着2 小时前
PAT 1164 Good in C
c语言·开发语言
Xの哲學3 小时前
Linux设备管理:从内核驱动到用户空间的完整架构解析
linux·服务器·算法·架构·边缘计算