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;
}
相关推荐
Re.不晚1 小时前
挑战做100道力扣算法- DAY1
算法·leetcode·职场和发展
蓝悦无人机2 小时前
《Planning algorithms》读书笔记——第1章 引言
算法·读书笔记·规划算法·lavalle
2601_955518182 小时前
C语言标准演化史:从K&R到GNU,谁才是正统?
c语言·gnu·标准演化·ansic·性能与可移植性
Sagittarius_A*2 小时前
分组密码基础(二):Feistel 结构与 DES 的设计思想
算法·信息安全·密码学·des·数论
青山木2 小时前
Hot 100 --- 搜索插入位置
java·数据结构·算法·leetcode
alexwang2113 小时前
HDU 4348 详细题解
c++·算法·题解·hdu·主席树·可持久化数据结构·可持久化线段树
程序员zgh3 小时前
C++ 拷贝赋值运算符 详解
c语言·开发语言·c++
可编程芯片开发3 小时前
基于分段变步长搜索RMDCFT算法的天基雷达空间机动目标检测方法MATLAB仿真,对比RMDCFT算法
算法
2501_946736164 小时前
在线考试平台如何选型?从功能、优势与应用场景角度详解
大数据·人工智能·算法
天辛大师4 小时前
天心大师:不确定中锚定自我,AI生活的哲学命题
人工智能·算法·决策树·机器学习·生活·启发式算法