LeetCode 3359: Find Sorted Submatrices With Maximum Element at Most K Solution
Master LeetCode problem 3359 (Find Sorted Submatrices With Maximum Element at Most K), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
3359. Find Sorted Submatrices With Maximum Element at Most K
Problem Explanation
Explanation:
To solve this problem, we can utilize the concept of dynamic programming and binary search. We will iterate over all possible columns pairs and maintain a 1D array representing the sum of elements in these columns. Then, for each pair of rows, we will find the subarray with the maximum sum that is less than or equal to k using binary search. By doing this for all possible pairs of rows and columns, we can find the count of submatrices with the maximum element at most k.
Algorithm:
- Initialize a variable
resultto store the count of submatrices with the maximum element at most k. - Iterate over all possible pairs of columns
c1andc2. - For each pair of columns, initialize a 1D array
colSumto store the sum of elements between these columns for each row. - Iterate over all pairs of rows
r1andr2. - Compute the sum of elements between columns
c1andc2for each row betweenr1andr2and store it in thecolSumarray. - For each row pair, find the subarray with the maximum sum that is less than or equal to k using binary search.
- Increment the
resultby the number of valid submatrices found in step 6. - Return the
result.
Time Complexity:
The time complexity of this approach is O(n^2 * m * log(m)), where n is the number of rows and m is the number of columns in the input matrix.
Space Complexity:
The space complexity is O(m) for storing the colSum array.
: :
Solution Code
class Solution {
public int numSubmatrixSumTarget(int[][] matrix, int k) {
int n = matrix.length;
int m = matrix[0].length;
int result = 0;
for (int c1 = 0; c1 < m; c1++) {
int[] colSum = new int[n];
for (int c2 = c1; c2 < m; c2++) {
for (int r = 0; r < n; r++) {
colSum[r] += matrix[r][c2];
}
TreeSet<Integer> set = new TreeSet<>();
set.add(0);
int prefixSum = 0;
for (int sum : colSum) {
prefixSum += sum;
Integer target = set.ceiling(prefixSum - k);
if (target != null) {
result += set.subSet(target, prefixSum).size();
}
set.add(prefixSum);
}
}
}
return result;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3359 (Find Sorted Submatrices With Maximum Element at Most K)?
This page provides optimized solutions for LeetCode problem 3359 (Find Sorted Submatrices With Maximum Element at Most K) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 3359 (Find Sorted Submatrices With Maximum Element at Most K)?
The time complexity for LeetCode 3359 (Find Sorted Submatrices With Maximum Element at Most K) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3359 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3359 in Java, C++, or Python.