avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

2679.In the matrix and the harmony

  2024-01-01        
字数统计: 331字   |   阅读时长: 2min

topic:

2023-07-05.png
2679.In the matrix and the harmony.md

Thought:

One -line
First of all, the meaning is to find the largest number of each sub -list,Thenpopgo out,Finally ask for peace。
The effect of traversing again and again is too bad,So I thought of using itzip一次性Traversal多个子Array。
于yes先对每个子ArraySort,Then usezipTraversal,Find the maximum。
for example:
nums = [[7,2,1],[6,4,2],[6,5,3],[3,2,1]]
Sort后:
nums = [[1,2,7],[2,4,6],[3,5,6],[1,2,3]]
Then usezipTraversal得到:
[(1,2,3,1),(2,4,5,2),(7,6,6,3)]
Find the maximum:
[3,5,7]
Context:
15

Code:

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

*The role and the rolezip()explain

1
2
3
4
5
6
7
8
9
10
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()The function corresponds to one -to -one elements in multiple lists,Then return onezipObject,Can uselist()Function convert to list。

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

*numsThe role ispythonNot a pointer,InsteadnumsEach element in the parameter is passed into the function。I understand here as a list。

1
2
3
4

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

zip(*nums)WillnumsEach element is passed in as a parameterzip()In the function,Then return onezipObject,Can uselist()Function convert to list。
zip(*nums)Equivalent tozip(num1, num2, num3, num4),innum1, num2, num3, num4yesnumsElement。

1
2
3
4
5
6
for i in zip(*nums):
print(i)
# Equivalent
#(1, 2, 3, 1)
#(2, 4, 5, 2)
#(7, 6, 6, 3)
  • Python
  • answer
  • Array
  • matrix
  • Sort
  • simulation
  • heap(Priority queue)

扫一扫,分享到微信

微信分享二维码
2639. Query the width of each column in the grid diagram
271. Code and decoding of string - Python Add the transposition symbol solution method Dual complexityO(n)
目录
  1. 1. topic:
  2. 2. Thought:
  3. 3. Code:
    1. 3.0.1. *The role and the rolezip()explain

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号