2500. Delete Greatest Value in Each Row Easy
1/**
2 * [2500] Delete Greatest Value in Each Row
3 *
4 * You are given an m x n matrix grid consisting of positive integers.
5 * Perform the following operation until grid becomes empty:
6 *
7 * Delete the element with the greatest value from each row. If multiple such elements exist, delete any of them.
8 * Add the maximum of deleted elements to the answer.
9 *
10 * Note that the number of columns decreases by one after each operation.
11 * Return the answer after performing the operations described above.
12 *
13 * <strong class="example">Example 1:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2022/10/19/q1ex1.jpg" style="width: 600px; height: 135px;" />
15 * Input: grid = [[1,2,4],[3,3,1]]
16 * Output: 8
17 * Explanation: The diagram above shows the removed values in each step.
18 * - In the first operation, we remove 4 from the first row and 3 from the second row (notice that, there are two cells with value 3 and we can remove any of them). We add 4 to the answer.
19 * - In the second operation, we remove 2 from the first row and 3 from the second row. We add 3 to the answer.
20 * - In the third operation, we remove 1 from the first row and 1 from the second row. We add 1 to the answer.
21 * The final answer = 4 + 3 + 1 = 8.
22 *
23 * <strong class="example">Example 2:
24 * <img alt="" src="https://assets.leetcode.com/uploads/2022/10/19/q1ex2.jpg" style="width: 83px; height: 83px;" />
25 * Input: grid = [[10]]
26 * Output: 10
27 * Explanation: The diagram above shows the removed values in each step.
28 * - In the first operation, we remove 10 from the first row. We add 10 to the answer.
29 * The final answer = 10.
30 *
31 *
32 * Constraints:
33 *
34 * m == grid.length
35 * n == grid[i].length
36 * 1 <= m, n <= 50
37 * 1 <= grid[i][j] <= 100
38 *
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/delete-greatest-value-in-each-row/
43// discuss: https://leetcode.com/problems/delete-greatest-value-in-each-row/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48 pub fn delete_greatest_value(grid: Vec<Vec<i32>>) -> i32 {
49 0
50 }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_2500() {
61 }
62}
63
Back
© 2025 bowen.ge All Rights Reserved.