首页 > 聚焦 > 正文

LeetCode 2679. Sum in a Matrix

2023-05-21 16:00:26   来源:哔哩哔哩  

You are given a 0-indexed 2D integer array nums. Initially, your score is 0. Perform the following operations until the matrix becomes empty:

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.

Identify the highest number amongst all those removed in step 1. Add that number to your score.


(资料图片)

Return the final score.

Example 1:

Input: nums = [[7,2,1],[6,4,2],[6,5,3],[3,2,1]]

Output: 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.

Example 2:

Input: nums = [[1]]Output: 1

Explanation: We remove 1 and add it to the answer. We return 1.

给你一个下标从 0 开始的二维整数数组 nums 。一开始你的分数为 0 。你需要执行以下操作直到矩阵变为空:

矩阵中每一行选取最大的一个数,并删除它。如果一行中有多个最大的数,选择任意一个并删除。

在步骤 1 删除的所有数字中找到最大的一个数字,将它添加到你的 分数 中。

请你返回最后的 分数 。

可以先将每一行数组进行排序,然后依次求出每一列的最大值,sum求和记录即可;

Runtime: 16 ms, faster than 88.62% of Java online submissions for Sum in a Matrix.

Memory Usage: 57 MB, less than 99.68% of Java online submissions for Sum in a Matrix.

关键词:

推荐内容

Copyright www.caikuang.rexun.cn 版权所有
网站备案号:豫ICP备20005723号-6
邮箱:29 59 11 57 8@qq.com