Team Ai
Datasetpublic

FPEvalRepoPublic/LeetCodeMetaData

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes399downloads
maximum-sum-with-at-most-k-elements.json39 linesDownload Raw Back to root
1{2  "id": 3764,3  "name": "maximum-sum-with-at-most-k-elements",4  "difficulty": "Medium",5  "link": "https://leetcode.com/problems/maximum-sum-with-at-most-k-elements/",6  "date": "2025-02-16",7  "task_description": "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`. **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))`",8  "test_case": [9    {10      "label": "Example 1",11      "input": "grid = [[1,2],[3,4]], limits = [1,2], k = 2",12      "output": "7 "13    },14    {15      "label": "Example 2",16      "input": "grid = [[5,3,7],[8,2,6]], limits = [2,2], k = 3",17      "output": "21 "18    }19  ],20  "constraints": [21    "The number of elements taken from the ith row of grid does not exceed limits[i].",22    "From the second row, we can take at most 2 elements. The elements taken are 4 and 3.",23    "The maximum possible sum of at most 2 selected elements is 4 + 3 = 7.",24    "From the first row, we can take at most 2 elements. The element taken is 7.",25    "From the second row, we can take at most 2 elements. The elements taken are 8 and 6.",26    "The maximum possible sum of at most 3 selected elements is 7 + 8 + 6 = 21.",27    "n == grid.length == limits.length",28    "m == grid[i].length",29    "1 <= n, m <= 500",30    "0 <= grid[i][j] <= 105",31    "0 <= limits[i] <= m",32    "0 <= k <= min(n * m, sum(limits))"33  ],34  "python_template": "class Solution(object):\n    def maxSum(self, grid, limits, k):\n        \"\"\"\n        :type grid: List[List[int]]\n        :type limits: List[int]\n        :type k: int\n        :rtype: int\n        \"\"\"\n        ",35  "java_template": "class Solution {\n    public long maxSum(int[][] grid, int[] limits, int k) {\n        \n    }\n}",36  "metadata": {37    "func_name": "maxSum"38  }39}