2679. Sum in a Matrix Medium

@problem@discussion
#Array#Sorting#Heap (Priority Queue)#Matrix#Simulation



1/**
2 * [2679] Sum in a Matrix
3 *
4 * You are given a 0-indexed 2D integer array nums. Initially, your score is 0. Perform the following operations until the matrix becomes empty:
5 * <ol>
6 * 	From each row in the matrix, select the largest number and remove it. In the case of a tie, it does not matter which number is chosen.
7 * 	Identify the highest number amongst all those removed in step 1. Add that number to your score.
8 * </ol>
9 * Return the final score.
10 *  
11 * Example 1:
12 * 
13 * Input: nums = [[7,2,1],[6,4,2],[6,5,3],[3,2,1]]
14 * Output: 15
15 * Explanation: In the first operation, we remove 7, 6, 6, and 3. We then add 7 to our score. Next, we remove 2, 4, 5, and 2. We add 5 to our score. Lastly, we remove 1, 2, 3, and 1. We add 3 to our score. Thus, our final score is 7 + 5 + 3 = 15.
16 * 
17 * Example 2:
18 * 
19 * Input: nums = [[1]]
20 * Output: 1
21 * Explanation: We remove 1 and add it to the answer. We return 1.
22 *  
23 * Constraints:
24 * 
25 * 	1 <= nums.length <= 300
26 * 	1 <= nums[i].length <= 500
27 * 	0 <= nums[i][j] <= 10^3
28 * 
29 */
30pub struct Solution {}
31
32// problem: https://leetcode.com/problems/sum-in-a-matrix/
33// discuss: https://leetcode.com/problems/sum-in-a-matrix/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37impl Solution {
38    pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
39        0
40    }
41}
42
43// submission codes end
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_2679() {
51    }
52}
53


Back
© 2025 bowen.ge All Rights Reserved.