1758. 生成交替二进制字符串的最少操作数
This commit is contained in:
parent
f8fd02dcb8
commit
566c6a8414
58
src/main.rs
58
src/main.rs
|
|
@ -1,51 +1,33 @@
|
|||
use std::cmp::min;
|
||||
|
||||
struct Solution;
|
||||
mod arr;
|
||||
impl Solution {
|
||||
pub fn num_special(mat: Vec<Vec<i32>>) -> i32 {
|
||||
let mut disable = vec![false; mat[0].len()];
|
||||
let mut arr = vec![0; mat[0].len()];
|
||||
for i in 0..mat.len() {
|
||||
let mut disable_i = false;
|
||||
let mut next_do_disable = false;
|
||||
let mut last_index = 1000_usize;
|
||||
for j in 0..mat[0].len() {
|
||||
if mat[i][j] == 1 {
|
||||
if next_do_disable {
|
||||
disable_i = true;
|
||||
arr[last_index] = 0;
|
||||
disable[last_index] = true;
|
||||
disable[j] = true;
|
||||
arr[j] = 0;
|
||||
}
|
||||
if disable[j] {
|
||||
disable_i = true;
|
||||
continue;
|
||||
}
|
||||
if disable_i {
|
||||
disable[j] = true;
|
||||
if arr[j] != 0 {
|
||||
arr[j] = 0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if arr[j] != 0 {
|
||||
disable[j] = true;
|
||||
arr[j] = 0;
|
||||
disable_i = true;
|
||||
continue
|
||||
}
|
||||
arr[j] += 1;
|
||||
next_do_disable = true;
|
||||
last_index = j;
|
||||
pub fn min_operations(s: String) -> i32 {
|
||||
let (mut cnt_a, mut cnt_b) = (0, 0);
|
||||
let mut cnt = 0;
|
||||
for c in s.chars() {
|
||||
if cnt & 1 == 1 {
|
||||
if c == '0' {
|
||||
cnt_a += 1;
|
||||
} else {
|
||||
cnt_b += 1;
|
||||
}
|
||||
} else {
|
||||
if c == '0' {
|
||||
cnt_b += 1;
|
||||
} else {
|
||||
cnt_a += 1;
|
||||
}
|
||||
}
|
||||
cnt += 1;
|
||||
}
|
||||
arr.iter().sum()
|
||||
min(cnt_a, cnt_b)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let result = Solution::num_special(arr::make_matrix("[[0,0,0,0,0,0],[0,0,1,0,0,0],[0,0,0,0,1,0],[0,0,0,0,0,0],[0,0,1,0,0,1],[0,0,0,0,0,0],[0,0,0,0,0,0]]"));
|
||||
let result = Solution::min_operations("1111".to_string());
|
||||
println!("{:?}", result);
|
||||
}
|
||||
// [[0,0,0,0,0,0],
|
||||
|
|
|
|||
Loading…
Reference in New Issue