-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc1072.rs
77 lines (62 loc) · 1.86 KB
/
lc1072.rs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// LeetCode problem 1072: Flip Columns For Maximum Number of Equal Rows
// https://leetcode.com/problems/flip-columns-for-maximum-number-of-equal-rows/description/
fn main() {
let result = max_equal_rows_after_flips(vec![vec![0, 1], vec![1, 1]]);
println!("Result = {result}");
}
fn max_equal_rows_after_flips(matrix: Vec<Vec<i32>>) -> i32 {
// try set each row to all zeroes, test for max
let mut maximum = 0;
for row in matrix.iter() {
let mut change_rows = vec![false; row.len()];
for (idx, element) in row.iter().enumerate() {
if *element == 1 {
change_rows[idx] = true;
}
}
let consistent_rows = test_change(matrix.clone(), &change_rows);
maximum = std::cmp::max(maximum, consistent_rows);
}
return maximum;
}
fn test_change(mut matrix: Vec<Vec<i32>>, idxs: &Vec<bool>) -> i32 {
let mut rows_matching = 0;
for row in matrix.iter_mut() {
let mut all_match = true;
if idxs[0] {
row[0] ^= 1;
}
for i in 1..row.len() {
if idxs[i] {
row[i] ^= 1;
}
if row[i] != row[i - 1] {
all_match = false;
break;
}
}
if all_match {
rows_matching += 1;
}
}
return rows_matching;
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn example_one() {
let result = max_equal_rows_after_flips(vec![vec![0, 1], vec![1, 1]]);
assert_eq!(result, 1);
}
#[test]
fn example_two() {
let result = max_equal_rows_after_flips(vec![vec![0, 1], vec![1, 0]]);
assert_eq!(result, 2);
}
#[test]
fn example_three() {
let result = max_equal_rows_after_flips(vec![vec![0, 0, 0], vec![0, 0, 1], vec![1, 1, 0]]);
assert_eq!(result, 2);
}
}