当前位置:网站首页>1605. Sum the feasible matrix for a given row and column
1605. Sum the feasible matrix for a given row and column
2022-07-06 16:08:00 【mrbone9】
Address :
Power button
https://leetcode-cn.com/problems/find-valid-matrix-given-row-and-column-sums/
subject :
Here are two arrays of nonnegative integers rowSum and colSum , among rowSum[i] Is the second in a two-dimensional matrix i Sum of row elements , colSum[j] It's No j The sum of column elements . In other words, you don't know every element in the matrix , But you know the sum of every row and column .
Please find the size of rowSum.length x colSum.length Arbitrarily Non-negative integer matrix , And the matrix satisfies rowSum and colSum The requirements of .
Please return any two-dimensional matrix that meets the requirements of the topic , The problem is guaranteed to exist At least one Feasible matrix .
Example 1:
| Input :rowSum = [3,8], colSum = [4,7] Output :[[3,0], [1,7]] explain : The first 0 That's ok :3 + 0 = 3 == rowSum[0] The first 1 That's ok :1 + 7 = 8 == rowSum[1] The first 0 Column :3 + 1 = 4 == colSum[0] The first 1 Column :0 + 7 = 7 == colSum[1] The sum of rows and columns meets the requirements of the title , And all matrix elements are nonnegative . Another feasible matrix is :[[1,2], [3,5]] |
Example 2:
| Input :rowSum = [5,7,10], colSum = [8,6,8] Output :[[0,5,0], [6,1,0], [2,0,8]] |
Example 3:
| Input :rowSum = [14,9], colSum = [6,9,8] Output :[[0,9,5], [6,0,3]] |
Example 4:
| Input :rowSum = [1,0], colSum = [1] Output :[[1], [0]] |
Example 5:
| Input :rowSum = [0], colSum = [0] Output :[[0]] |
Tips :
| 1 <= rowSum.length, colSum.length <= 500 0 <= rowSum[i], colSum[i] <= 108 sum(rows) == sum(columns) |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/find-valid-matrix-given-row-and-column-sums
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
Push backward to , The element takes the minimum value of the row and column , The maximum value will explode
When the element is determined , Row sum And Column sum The value will change accordingly
Repeat this process for the remaining elements
Method 1 、 Push backward
#define min(a,b) ( (a) < (b) ? (a) : (b) )
int **myMalloc(int r, int c, int *return_r, int **return_c)
{
int **ret = (int **)malloc(sizeof(int *) * r);
*return_r = r;
*return_c =(int *)malloc(sizeof(int) * r);
for(int i=0; i<r; i++)
{
ret[i] = (int *)malloc(sizeof(int) * c);
(*return_c)[i] = c;
}
return ret;
}
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** restoreMatrix(int* rowSum, int rowSumSize, int* colSum, int colSumSize, int* returnSize, int** returnColumnSizes){
int row = rowSumSize;
int col = colSumSize;
int i, j;
int **grid = myMalloc(row, col, returnSize, returnColumnSizes);
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
grid[i][j] = min(rowSum[i], colSum[j]);
rowSum[i] -= grid[i][j];
colSum[j] -= grid[i][j];
}
}
return grid;
}边栏推荐
- 【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)
- 【练习-7】(Uva 10976)Fractions Again?!(分数拆分)
- Opencv learning log 14 - count the number of coins in the picture (regardless of overlap)
- Research Report on surgical fluid treatment industry - market status analysis and development prospect prediction
- 2078. Two houses with different colors and the farthest distance
- Find 3-friendly Integers
- Information security - threat detection - detailed design of NAT log access threat detection platform
- [exercise-7] crossover answers
- [exercise-9] Zombie's Treasury test
- 【高老师软件需求分析】20级云班课习题答案合集
猜你喜欢

【高老师软件需求分析】20级云班课习题答案合集
![[exercise-7] crossover answers](/img/66/3dcba2e70a4cd899fbd78ce4d5bea2.png)
[exercise-7] crossover answers

Borg maze (bfs+ minimum spanning tree) (problem solving report)

7-1 懂的都懂 (20 分)

2078. Two houses with different colors and the farthest distance

树莓派4B安装opencv3.4.0

Data storage in memory & loading into memory to make the program run

1005. Maximized array sum after K negations

Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
![[exercise-5] (UVA 839) not so mobile (balance)](/img/8e/48dcf75f7347b36301df6fc129c09d.png)
[exercise-5] (UVA 839) not so mobile (balance)
随机推荐
[exercise-7] (UVA 10976) fractions again?! (fraction split)
信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
B - 代码派对(女生赛)
Opencv learning log 24 -- Hough transform 2 (maximum interval and minimum length can be limited)
Research Report on market supply and demand and strategy of China's earth drilling industry
【练习-7】Crossword Answers
STM32 learning record: LED light flashes (register version)
【高老师UML软件建模基础】20级云班课习题答案合集
Quick to typescript Guide
[exercise 4-1] cake distribution
7-1 懂的都懂 (20 分)
滲透測試 ( 1 ) --- 必備 工具、導航
信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
[exercise-5] (UVA 839) not so mobile (balance)
TCP's three handshakes and four waves
China potato slicer market trend report, technical dynamic innovation and market forecast
What is the difficulty of programming?
Flink 使用之 CEP
Frida hook so layer, protobuf data analysis
Information security - security professional name | CVE | rce | POC | Vul | 0day