Skip to content

Commit

Permalink
Create 29-reconstruct-a-2-row-binary-matrix.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 29, 2023
1 parent 8127ea3 commit 06d14c4
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 2023/06/29-reconstruct-a-2-row-binary-matrix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
impl Solution {
pub fn reconstruct_matrix(mut upper: i32, mut lower: i32, colsum: Vec<i32>) -> Vec<Vec<i32>> {
let mut ans = vec![vec![]; 2];
for x in colsum {
match x {
0 => {
ans[0].push(0);
ans[1].push(0);
}
1 => {
if upper > lower {
ans[0].push(1);
ans[1].push(0);
upper -= 1;
} else {
ans[0].push(0);
ans[1].push(1);
lower -= 1;
}
}
2 => {
ans[0].push(1);
ans[1].push(1);
upper -= 1;
lower -= 1;
}
_ => unreachable!(),
}
}
if upper != 0 || lower != 0 {
return vec![];
}
ans
}
}

0 comments on commit 06d14c4

Please sign in to comment.