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;
}
相关推荐
菜鸡爱玩15 小时前
线性代数矩阵相乘
线性代数·算法·矩阵
devilnumber19 小时前
Java 递归算法 详解 + 核心要点 + 实战运用 + 避坑指南
java·开发语言·算法
‎ദ്ദിᵔ.˛.ᵔ₎21 小时前
双指针、滑动窗口、前缀和、二分查找 算法
算法
顾北顾21 小时前
多头注意力机制
人工智能·深度学习·算法
H1785350909621 小时前
SolidWorks_基于草图的实体特征20_特征错误排查
算法·3d建模·solidworks
hujinyuan2016021 小时前
2025年12月中国电子学会青少年机器人技术等级考试试卷(二级) 真题+答案
人工智能·算法·机器人
玖玥拾21 小时前
C/C++ 基础笔记(十三)继承
c语言·c++·继承
bIo7lyA8v1 天前
算法复杂度评估的实验统计方法与可视化的技术8
算法
李老师讲编程1 天前
中国电子学会图形化2020.12月Scratch三级考级题
算法·scratch·信息学奥赛·图形化编程·scratch素材
退休倒计时1 天前
【每日一题】LeetCode 53. 最大子数组和 TypeScript
数据结构·算法·leetcode·typescript