内卷地狱

2679. Sum in a Matrix

Edit Me

Problem

2679. Sum in a Matrix

Approach

One-liner

The idea is to find the largest number from each sub-list, pop it out, then sum those values. Traversing repeatedly would be inefficient, so I thought of using zip to traverse multiple sub-arrays at once.

First, sort each sub-array, then use zip to traverse column by column and find the maximum.

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

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

Then using zip:
[(1,2,3,1),(2,4,5,2),(7,6,6,3)]

Find the maximum of each column:
[3,5,7]

Sum:
15

Code

class Solution:
    def matrixSum(self, nums: List[List[int]]) -> int:
        return sum(max(i) for i in \
        zip(*(sorted(sublist) for sublist in nums)))

Explanation of * and zip()

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

for i in range(len(nums[1])):
    for j in range(len(nums)):
        print(nums[j][i])
# ans = 123124527663
num1 = [1,2,7]
num2 = [2,4,6]
num3 = [3,5,6]
num4 = [1,2,3]

zip() pairs elements from multiple lists one-to-one, returning a zip object that can be converted to a list using list().

for i in zip(num1, num2, num3, num4):
    print(i)
#(1, 2, 3, 1)
#(2, 4, 5, 2)
#(7, 6, 6, 3)

*nums in Python is not a pointer — it unpacks each element of nums as a separate argument into the function.

# Unpacking
print(*nums)
# [1, 2, 7] [2, 4, 6] [3, 5, 6] [1, 2, 3]

zip(*nums) passes each element of nums as a separate argument to zip(), equivalent to zip(num1, num2, num3, num4).

for i in zip(*nums):
    print(i)
# Equivalent
#(1, 2, 3, 1)
#(2, 4, 5, 2)
#(7, 6, 6, 3)

贡献者


这篇文章有帮助吗?

最近更新

Involution Hell© 2026 byCommunityunderCC BY-NC-SA 4.0CCBYNCSA