返回介绍

solution / 2600-2699 / 2679.Sum in a Matrix / README_EN

发布于 2024-06-17 01:03:01 字数 4394 浏览 0 评论 0 收藏 0

2679. Sum in a Matrix

中文文档

Description

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

  1. 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.
  2. 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.

 

Constraints:

  • 1 <= nums.length <= 300
  • 1 <= nums[i].length <= 500
  • 0 <= nums[i][j] <= 103

Solutions

Solution 1

class Solution:
  def matrixSum(self, nums: List[List[int]]) -> int:
    for row in nums:
      row.sort()
    return sum(map(max, zip(*nums)))
class Solution {
  public int matrixSum(int[][] nums) {
    for (var row : nums) {
      Arrays.sort(row);
    }
    int ans = 0;
    for (int j = 0; j < nums[0].length; ++j) {
      int mx = 0;
      for (var row : nums) {
        mx = Math.max(mx, row[j]);
      }
      ans += mx;
    }
    return ans;
  }
}
class Solution {
public:
  int matrixSum(vector<vector<int>>& nums) {
    for (auto& row : nums) {
      sort(row.begin(), row.end());
    }
    int ans = 0;
    for (int j = 0; j < nums[0].size(); ++j) {
      int mx = 0;
      for (auto& row : nums) {
        mx = max(mx, row[j]);
      }
      ans += mx;
    }
    return ans;
  }
};
func matrixSum(nums [][]int) (ans int) {
  for _, row := range nums {
    sort.Ints(row)
  }
  for i := 0; i < len(nums[0]); i++ {
    mx := 0
    for _, row := range nums {
      mx = max(mx, row[i])
    }
    ans += mx
  }
  return
}
function matrixSum(nums: number[][]): number {
  for (const row of nums) {
    row.sort((a, b) => a - b);
  }
  let ans = 0;
  for (let j = 0; j < nums[0].length; ++j) {
    let mx = 0;
    for (const row of nums) {
      mx = Math.max(mx, row[j]);
    }
    ans += mx;
  }
  return ans;
}
impl Solution {
  pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
    let mut nums = nums;
    for row in nums.iter_mut() {
      row.sort();
    }
    let transposed: Vec<Vec<i32>> = (0..nums[0].len())
      .map(|i| {
        nums.iter()
          .map(|row| row[i])
          .collect()
      })
      .collect();

    transposed
      .iter()
      .map(|row| row.iter().max().unwrap())
      .sum()
  }
}

Solution 2

impl Solution {
  pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
    let mut nums = nums.clone();
    for row in nums.iter_mut() {
      row.sort();
    }

    let mut ans = 0;
    for j in 0..nums[0].len() {
      let mut mx = 0;
      for row in &nums {
        mx = mx.max(row[j]);
      }
      ans += mx;
    }

    ans
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文