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

1253.Reconstruct 2 Line binary matrix

  2024-01-01        
字数统计: 315字   |   阅读时长: 1min

topic:

[1253]Reconstruct 2 Line binary matrix.md
Reconstruct 2 Line binary matrix

Thought:

At first I read the wrong question again,Thought it wascolsumDivideupper and lower两个Array,
The result iscolsum[i]Divideupper[i] and lower[i]Two numbers。
andupper[i]andlower[i]的and等于colsum[i]。
In each cycle,ObtaincolsumValue,Then judge0,1,2仨 仨,0Ignore,2If you allocate it on average。
1if,直接给当前最小的那个Array。(I use hereupperandlowerThe parameters of itself to record the remaining numbers)。
Determine at the beginningif sum(colsum) != upper + lower, Final judgmentif upper + lower != 0。

At first, I misunderstood the question again, thinking that it was about dividing colsum into two arrays, upper and lower. However, it turned out that it was about dividing colsum[i] into two numbers, upper[i] and lower[i]. Additionally, the sum of upper[i] and lower[i] should be equal to colsum[i].
In each iteration, the value of colsum is obtained, and then three scenarios, 0, 1, and 2, are considered. If it’s 0, it is ignored. If it’s 2, the values are evenly distributed. If it’s 1, it is assigned to the array with the current minimum value (using the remaining numbers in upper and lower than parameters).
At the beginning, it is checked whether if sum(colsum) != upper + lower, and finally, it is checked whether if upper + lower != 0.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
def reconstructMatrix(self, upper: int, lower: int, colsum: List[int]) -> List[List[int]]:
if sum(colsum) != upper + lower:
return []
upper_list = [0] * len(colsum)
lower_list = [0] * len(colsum)
for i in range(len(colsum)):
if colsum[i] == 0:
continue
if colsum[i] == 2:
upper_list[i] = 1
lower_list[i] = 1
upper -= 1
lower -= 1
else:
if upper >= lower:
upper_list[i] = 1
upper -= 1
else:
lower_list[i] = 1
lower -= 1
if upper != 0 or lower != 0:
return []
return [upper_list, lower_list]
  • Python
  • answer
  • Array
  • matrix
  • greedy
  • medium

扫一扫,分享到微信

微信分享二维码
1333.Restaurant filter
1460.Class ScheduleIV
目录
  1. 1. topic:
  2. 2. Thought:
  3. 3. Code:

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