当前位置:网站首页>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 :
边栏推荐
- Static page of pinyougou mall
- 01-分布式系统概述
- 两道面试小Demo
- Key summary VII of PMP examination - monitoring process group (1)
- Regular verification of mobile phone number and email [easy to understand]
- SQL optimization experience: from 30248 seconds to 0.001 seconds
- Is it safe for Huatai Securities to open an account online? What is the handling process
- Screen settings in the source code of OBS Live Room
- Virtual machine 14 installing win7 (Figure tutorial)
- 异常处理4种方法
猜你喜欢

布隆过滤器 课程研究报告

Full link service tracking implementation scheme

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

Data visualization makes correlation analysis easier to use
理解IO模型

Common test method used by testers --- orthogonal method
![[ybtoj advanced training guidance] class roll call [string hash]](/img/5b/bbf8fa51d180b50fbbee4bcc278c70.jpg)
[ybtoj advanced training guidance] class roll call [string hash]

1182: effets de la photo de groupe

Calcul des frais d'achat et de vente d'actions

Data mining modeling practice
随机推荐
Methods for creating multithreads ---1 creating subclasses of thread class and multithreading principle
1182: group photo effect
How to reduce the risk of project communication?
[share OpenGL tutorial]
Stutter participle_ Principle of word breaker
Virtual machine 14 installing win7 (Figure tutorial)
Two interview demo
学习阿里如何进行数据指标体系的治理
PMP考试重点总结九——收尾
Importerror: no module named image [duplicate] - importerror: no module named image [duplicate]
Fastjason filter field
Custom exception classes and exercises
SQL 優化經曆:從 30248秒到 0.001秒的經曆
Screen settings in the source code of OBS Live Room
手机买同业存单基金开户选哪家证券公司比较好,比较安全呢
满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”
redis5.0的槽点迁移,随意玩(单机迁移集群)
Key summary IV of PMP examination - planning process group (2)
flink cep 跳过策略 AfterMatchSkipStrategy.skipPastLastEvent() 匹配过的不再匹配 碧坑指南
Test cases for learning the basic content of software testing (II)