474. 一和零
This commit is contained in:
parent
9648737245
commit
0babcf1472
|
|
@ -1,3 +1,9 @@
|
||||||
|
pub fn make_string_arr(s: &str) -> Vec<String> {
|
||||||
|
s.trim_matches(&['[', ']'][..])
|
||||||
|
.split(',')
|
||||||
|
.map(|s| s.trim_matches(&['"',' ']).to_string())
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
pub fn make_arr(s: &str) -> Vec<i32> {
|
pub fn make_arr(s: &str) -> Vec<i32> {
|
||||||
s.trim_matches(&['[', ']'][..])
|
s.trim_matches(&['[', ']'][..])
|
||||||
.split(',')
|
.split(',')
|
||||||
|
|
@ -72,4 +78,4 @@ pub fn make_matrix(s: &str) -> Vec<Vec<i32>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
rows
|
rows
|
||||||
}
|
}
|
||||||
|
|
|
||||||
67
src/main.rs
67
src/main.rs
|
|
@ -1,39 +1,50 @@
|
||||||
struct Solution;
|
use crate::arr::make_string_arr;
|
||||||
|
use std::cmp::max;
|
||||||
|
|
||||||
|
struct Solution;
|
||||||
|
mod arr;
|
||||||
impl Solution {
|
impl Solution {
|
||||||
fn gcd_euclid(mut a: i32, mut b: i32) -> i32 {
|
pub fn find_max_form(strs: Vec<String>, m: i32, n: i32) -> i32 {
|
||||||
while b != 0 {
|
let mut dp = vec![vec![0; m as usize + 1]; n as usize + 1];
|
||||||
let r = a % b;
|
fn get_count(str: &String) -> (i32, i32) {
|
||||||
a = b;
|
let (mut zeros, mut ones) = (0, 0);
|
||||||
b = r;
|
for c in str.chars() {
|
||||||
}
|
if c == '0' {
|
||||||
a
|
zeros += 1;
|
||||||
}
|
} else {
|
||||||
pub fn min_operations(nums: Vec<i32>) -> i32 {
|
ones += 1;
|
||||||
let mut ones = 0;
|
|
||||||
for i in 0..nums.len() {
|
|
||||||
if nums[i] == 1 {
|
|
||||||
ones += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ones > 0 {
|
|
||||||
return (nums.len() - ones) as i32;
|
|
||||||
}
|
|
||||||
for i in 2..=nums.len() {
|
|
||||||
for j in 0..nums.len() - i + 1 {
|
|
||||||
let mut gcd = nums[j];
|
|
||||||
for k in j + 1..j + i {
|
|
||||||
gcd = Self::gcd_euclid(gcd, nums[k]);
|
|
||||||
}
|
}
|
||||||
if gcd == 1 {
|
}
|
||||||
return nums.len() as i32 - 1 + i as i32 - 1;
|
(zeros, ones)
|
||||||
|
}
|
||||||
|
let mut strs = strs
|
||||||
|
.iter()
|
||||||
|
.map(|x| get_count(x))
|
||||||
|
.collect::<Vec<(i32, i32)>>();
|
||||||
|
// n1 m0
|
||||||
|
// 外1内0
|
||||||
|
|
||||||
|
for i in 0..strs.len() {
|
||||||
|
let (zeros, ones) = strs[i];
|
||||||
|
for i in (ones..=n).rev() {
|
||||||
|
for k in (zeros..=m).rev() {
|
||||||
|
dp[i as usize][k as usize] = dp[i as usize][k as usize]
|
||||||
|
.max(dp[(i - ones) as usize][(k - zeros) as usize] + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
-1
|
let mut ans = 0;
|
||||||
|
dp.iter()
|
||||||
|
.for_each(|x| x.iter().for_each(|y| ans = max(ans, *y)));
|
||||||
|
ans
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// dp?
|
||||||
fn main() {
|
fn main() {
|
||||||
let result = Solution::min_operations(vec![1,1]);
|
let result = Solution::find_max_form(
|
||||||
|
make_string_arr("[\"10\", \"0001\", \"111001\", \"1\", \"0\"]"),
|
||||||
|
5,
|
||||||
|
3,
|
||||||
|
);
|
||||||
println!("{:?}", result);
|
println!("{:?}", result);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue