3462. Maximum Sum With at Most K Elements LeetCode Solution

In this guide, you will get 3462. Maximum Sum With at Most K Elements LeetCode Solution with the best time and space complexity. The solution to Maximum Sum With at Most K Elements problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Sum With at Most K Elements solution in C++
  4. Maximum Sum With at Most K Elements solution in Java
  5. Maximum Sum With at Most K Elements solution in Python
  6. Additional Resources
3462. Maximum Sum With at Most K Elements LeetCode Solution image

Problem Statement of Maximum Sum With at Most K Elements

You are given a 2D integer matrix grid of size n x m, an integer array limits of length n, and an integer k. The task is to find the maximum sum of at most k elements from the matrix grid such that:

The number of elements taken from the ith row of grid does not exceed limits[i].

Return the maximum sum.

Example 1:

Input: grid = [[1,2],[3,4]], limits = [1,2], k = 2
Output: 7
Explanation:

From the second row, we can take at most 2 elements. The elements taken are 4 and 3.
The maximum possible sum of at most 2 selected elements is 4 + 3 = 7.

Example 2:

Input: grid = [[5,3,7],[8,2,6]], limits = [2,2], k = 3
Output: 21
Explanation:

From the first row, we can take at most 2 elements. The element taken is 7.
From the second row, we can take at most 2 elements. The elements taken are 8 and 6.
The maximum possible sum of at most 3 selected elements is 7 + 8 + 6 = 21.

See also  2068. Check Whether Two Strings are Almost Equivalent LeetCode Solution

Constraints:

n == grid.length == limits.length
m == grid[i].length
1 <= n, m <= 500
0 <= grid[i][j] <= 105
0 <= limits[i] <= m
0 <= k <= min(n * m, sum(limits))

Complexity Analysis

  • Time Complexity: O(nm\log nm)
  • Space Complexity: O(k)

3462. Maximum Sum With at Most K Elements LeetCode Solution in C++

class Solution {
 public:
  long long maxSum(vector<vector<int>>& grid, vector<int>& limits, int k) {
    long ans = 0;
    priority_queue<int, vector<int>, greater<>> minHeap;

    for (int i = 0; i < grid.size(); ++i) {
      ranges::sort(grid[i], greater<>());
      for (int j = 0; j < limits[i]; ++j) {
        minHeap.push(grid[i][j]);
        if (minHeap.size() == k + 1)
          minHeap.pop();
      }
    }

    while (!minHeap.empty())
      ans += minHeap.top(), minHeap.pop();

    return ans;
  }
};
/* code provided by PROGIEZ */

3462. Maximum Sum With at Most K Elements LeetCode Solution in Java

class Solution {
  public long maxSum(int[][] grid, int[] limits, int k) {
    long ans = 0;
    Queue<Integer> minHeap = new PriorityQueue<>();

    for (int i = 0; i < grid.length; ++i) {
      Arrays.sort(grid[i]);
      for (int j = grid[i].length - 1; j >= grid[i].length - limits[i]; --j) {
        minHeap.offer(grid[i][j]);
        if (minHeap.size() == k + 1)
          minHeap.poll();
      }
    }

    while (!minHeap.isEmpty())
      ans += minHeap.poll();

    return ans;
  }
}
// code provided by PROGIEZ

3462. Maximum Sum With at Most K Elements LeetCode Solution in Python

class Solution:
  def maxSum(self, grid: list[list[int]], limits: list[int], k: int) -> int:
    minHeap = []

    for row, limit in zip(grid, limits):
      row.sort(reverse=True)
      for i in range(limit):
        heapq.heappush(minHeap, row[i])
        if len(minHeap) == k + 1:
          heapq.heappop(minHeap)

    return sum(minHeap)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.