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;
}
相关推荐
格林威3 小时前
常规线扫描镜头有哪些类型?能做什么?
人工智能·深度学习·数码相机·算法·计算机视觉·视觉检测·工业镜头
很㗊4 小时前
C与C++---类型转换
c语言·开发语言
say_fall4 小时前
精通C语言(3. 自定义类型:联合体和枚举)
c语言·开发语言
迎風吹頭髮4 小时前
UNIX下C语言编程与实践38-UNIX 信号操作:signal 函数与信号捕获函数的编写
linux·c语言·unix
La Pulga5 小时前
【STM32】I2C通信—软件模拟
c语言·stm32·单片机·嵌入式硬件·mcu
程序员莫小特5 小时前
老题新解|大整数加法
数据结构·c++·算法
过往入尘土6 小时前
服务端与客户端的简单链接
人工智能·python·算法·pycharm·大模型
zycoder.6 小时前
力扣面试经典150题day1第一题(lc88),第二题(lc27)
算法·leetcode·面试
Dream it possible!6 小时前
LeetCode 面试经典 150_哈希表_存在重复元素 II(46_219_C++_简单)
leetcode·面试·散列表
蒙奇D索大6 小时前
【数据结构】考研数据结构核心考点:二叉排序树(BST)全方位详解与代码实现
数据结构·笔记·学习·考研·算法·改行学it