topic:
[1253]Reconstruct 2 Line binary matrix.md
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 | class Solution: |