-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind_Valid_Matrix_Given_Row_and_Column_Sums.java
More file actions
47 lines (46 loc) · 1.49 KB
/
Find_Valid_Matrix_Given_Row_and_Column_Sums.java
File metadata and controls
47 lines (46 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.*;
import java.io.*;
import java.lang.*;
public class Find_Valid_Matrix_Given_Row_and_Column_Sums {
class Solution {
public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
int nr = rowSum.length;
int nc = colSum.length;
HashSet<Integer> r_set = new HashSet<>();
HashSet<Integer> c_set = new HashSet<>();
int[][] mat = new int[nr][nc];
while(r_set.size() != nr && c_set.size() != nc)
{
int min_r = minIndex(rowSum , r_set);
int min_c = minIndex(colSum , c_set);
if(rowSum[min_r]<colSum[min_c])
{
mat[min_r][min_c] = rowSum[min_r];
colSum[min_c] = colSum[min_c] - rowSum[min_r];
r_set.add(min_r);
}
else
{
mat[min_r][min_c] = colSum[min_c];
rowSum[min_r] = rowSum[min_r] - colSum[min_c];
c_set.add(min_c);
}
}
return mat;
}
private int minIndex(int[] arr , HashSet<Integer> hs)
{
int min = Integer.MAX_VALUE;
int ind = 0;
for(int i = 0 ; i<arr.length ; i++)
{
if(arr[i]<min && !hs.contains(i))
{
min = arr[i];
ind = i;
}
}
return ind;
}
}
}