当前位置:网站首页>June 27, 2022: give a 01 string with a length of N. now please find two intervals so that the number of 1 is equal and the number of 0 is equal in the two intervals. The two intervals can intersect bu
June 27, 2022: give a 01 string with a length of N. now please find two intervals so that the number of 1 is equal and the number of 0 is equal in the two intervals. The two intervals can intersect bu
2022-06-28 09:33:00 【Fuda scaffold constructor's daily question】
2022-06-27: Give a length of n Of 01 strand , Now please find two intervals ,
So that in these two intervals ,1 The number of is equal ,0 The number of is also equal ,
These two intervals can intersect , But it can not overlap completely , That is, the left and right endpoints of the two intervals cannot be exactly the same .
Now please find the two longest intervals , Meet the above requirements .
From Baidu .
answer 2022-06-27:
This question is ingenious . Dynamic programming is not an ingenious way .
L0= Leftmost left 0 And rightmost 0 The length of ,L1= Leftmost left 1 And rightmost 1 The length of , seek L0 and L1 The maximum value of .
The code to use rust To write . The code is as follows :
use rand::Rng;
use std::collections::HashMap;
fn main() {
let n: i32 = 500;
let test_time: i32 = 10000;
println!(" Beginning of the test ");
for i in 0..test_time {
let size = rand::thread_rng().gen_range(0, n) + 2;
let mut arr = random_array(size);
let ans1 = longest1(&mut arr);
let ans2 = longest2(&mut arr);
if ans1 != ans2 {
println!(" Something went wrong !{}", i);
println!("ans1 = {}", ans1);
println!("ans2 = {}", ans2);
break;
}
}
println!(" End of test ");
}
fn longest1(arr: &mut Vec<i32>) -> i32 {
let mut map: HashMap<i32, HashMap<i32, i32>> = HashMap::new();
for i in 0..arr.len() as i32 {
let mut zero = 0;
let mut one = 0;
for j in i..arr.len() as i32 {
zero += if arr[j as usize] == 0 { 1 } else { 0 };
one += if arr[j as usize] == 1 { 1 } else { 0 };
map.entry(zero).or_insert(HashMap::new());
let mapzero = map.get_mut(&zero).unwrap();
mapzero.insert(one, if mapzero.get(&one) == None { 0 } else { 1 } + 1);
}
}
let mut ans = 0;
for (k, v) in map.iter() {
for (k2, v2) in v.iter() {
let num = *v2;
if num > 1 {
ans = get_max(ans, k + k2);
}
}
}
return ans;
}
fn get_max<T: Clone + Copy + std::cmp::PartialOrd>(a: T, b: T) -> T {
if a > b {
a
} else {
b
}
}
fn longest2(arr: &mut Vec<i32>) -> i32 {
let mut left_zero = -1;
let mut right_zero: i32 = -1;
let mut left_one = -1;
let mut right_one: i32 = -1;
for i in 0..arr.len() as i32 {
if arr[i as usize] == 0 {
left_zero = i;
break;
}
}
for i in 0..arr.len() as i32 {
if arr[i as usize] == 1 {
left_one = i;
break;
}
}
let mut i = arr.len() as i32 - 1;
while i >= 0 {
if arr[i as usize] == 0 {
right_zero = i;
break;
}
i -= 1;
}
let mut i = arr.len() as i32 - 1;
while i >= 0 {
if arr[i as usize] == 1 {
right_one = i;
break;
}
i -= 1;
}
let p1 = right_zero - left_zero;
let p2 = right_one - left_one;
return get_max(p1, p2);
}
// In order to test
fn random_array(len: i32) -> Vec<i32> {
let mut arr: Vec<i32> = vec![];
for _i in 0..len {
arr.push(rand::thread_rng().gen_range(0, 2));
}
return arr;
}
The results are as follows :
边栏推荐
- PMP考试重点总结八——监控过程组(2)
- Static page of pinyougou mall
- DolphinScheduler使用系统时间
- 1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation
- Key summary VII of PMP examination - monitoring process group (1)
- Dbeaver connects to kingbasees V8 (ultra detailed graphic tutorial)
- 在本类私有属性直接使用?new()在使用!!!
- Importerror: no module named image [duplicate] - importerror: no module named image [duplicate]
- Redis5.0 slot migration, free play (single machine migration cluster)
- Stutter participle_ Principle of word breaker
猜你喜欢
How to reduce the risk of project communication?
rman備份報ORA-19809 ORA-19804
基于宽表的数据建模
满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”
股票 停牌
Write a simple timeline
Music website design based on harmonyos (portal page)
SQL 優化經曆:從 30248秒到 0.001秒的經曆
Learn how Alibaba manages the data indicator system
This article explains in detail the difficult problems and solutions faced by 3D cameras
随机推荐
Key summary V of PMP examination - execution process group
1. Kimball dimension modeling of data warehouse: what is a fact table?
Scenario method and error recommendation method for learning basic content of software testing (2)
Differences between task parameter types inout and ref
rman備份報ORA-19809 ORA-19804
详解final、finally和finalize
From knowledge to wisdom: how far will the knowledge map go?
线程和进程
Xiaomi's payment company was fined 120000 yuan, involving the illegal opening of payment accounts, etc.: Lei Jun is the legal representative, and the products include MIUI wallet app
Multithreading concurrent parallel threaded process
Play SFTP upload file
PMP考试重点总结四——规划过程组(2)
Custom exception classes and exercises
[ybtoj advanced training guidance] class roll call [string hash]
How to reduce the risk of project communication?
微信小程序开发日志
虚拟机14安装win7(图教程)
Boundary value analysis method for learning basic content of software testing (2)
1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation
学习阿里如何进行数据指标体系的治理