Max Sum of Rectangle Report You are given a non-...

Créé le : 31 juillet 2025

Répondu en utilisant O3 par Chat01

Question

Max Sum of Rectangle

Report

You are given a non-empty 20 matrix matrix (MXN) and an integer K

Find the max sum of a rectangle in the matrix such that its sum is no larger than k

cout

Input

145442

The first line of input contains three integers M k separated by space. N and

Each of the next M lines contains N integers separated by space.

Output

145442

The output contains an integer representing the maximum sum as mentioned above.

Code in c++

Réflexion

Developing solution code

It seems like the user wants a C++ solution for the "Max Sum of Rectangle No Larger Than K" problem (LeetCode 363). While the input format is a bit unclear, I gather that the input consists of three integers, M, N, and K, followed by a matrix of size M x N.

I’ll write the code to efficiently solve it with a time complexity of O(min(M,N)^2 * max(M,N) log max(M,N)), considering the problem constraints.

I’ll focus on parsing the matrix as described.

Implementing algorithm

The goal is to implement the "Max Sum of Rectangle No Larger Than K" efficient algorithm using prefix sums. I'll use a set (TreeSet) to find the best subarray with a sum no larger than K. The solution has a complexity of roughly O(N^2 * M log M) or O(M^2 * N log N), depending on the dimensions of the matrix, and handles negative values.

I'll transpose the matrix if N > M to optimize. I’ll also provide a minimal explanation and code, handling edge cases like large numbers and negative values.

Thought for 13 seconds

Réponse

Below is an efficient C++ solution (uses prefix sums + ordered set).
Time complexity: O(min(M,N)2max(M,N)logmax(M,N))O(\min(M,N)^2 \cdot \max(M,N)\log \max(M,N)).
It reads M N K, then the M×N matrix, and prints the maximum rectangle sum ≤ K.

cpp
#include <bits/stdc++.h> using namespace std; static long long maxSubarrayNoLargerThanK(const vector<long long>& arr, long long K) { long long best = LLONG_MIN; long long prefix = 0; multiset<long long> pref; // use ordered container for lower_bound pref.insert(0); // empty prefix for (long long x : arr) { prefix += x; // Find smallest prev >= prefix - K ==> prefix - prev <= K and as large as possible auto it = pref.lower_bound(prefix - K); if (it != pref.end()) { best = max(best, prefix - *it); } pref.insert(prefix); } return best; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int M, N; long long K; if (!(cin >> M >> N >> K)) return 0; vector<vector<long long>> A(M, vector<long long>(N)); for (int i = 0; i < M; ++i) for (int j = 0; j < N; ++j) cin >> A[i][j]; // To optimize, iterate over the smaller dimension as columns. bool transposed = false; if (N > M) { // Transpose to make columns <= rows vector<vector<long long>> T(N, vector<long long>(M, 0)); for (int i = 0; i < M; ++i) for (int j = 0; j < N; ++j) T[j][i] = A[i][j]; A.swap(T); swap(M, N); // now M >= N transposed = true; // not used further, but kept for clarity } long long answer = LLONG_MIN; // Fix left and right column boundaries and reduce to 1D problem on rows. for (int left = 0; left < N; ++left) { vector<long long> rowSums(M, 0); for (int right = left; right < N; ++right) { for (int r = 0; r < M; ++r) rowSums[r] += A[r][right]; long long bestHere = maxSubarrayNoLargerThanK(rowSums, K); answer = max(answer, bestHere); if (answer == K) { // Can't do better than exactly K cout << answer << '\n'; return 0; } } } cout << answer << '\n'; return 0; }

How it works (briefly):

  • Fix a pair of columns [left, right], compress each row to the sum between these columns → a 1D array.
  • For that 1D array, use prefix sums and a multiset to find the maximum subarray sum ≤ K.
  • Repeat over all column pairs (choosing the smaller dimension for columns to keep it fast).

Partager cette Q&R