当前位置:网站首页>2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠
2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠
2022-06-28 09:22:00 【福大大架构师每日一题】
2022-06-27:给出一个长度为n的01串,现在请你找到两个区间,
使得这两个区间中,1的个数相等,0的个数也相等,
这两个区间可以相交,但是不可以完全重叠,即两个区间的左右端点不可以完全一样。
现在请你找到两个最长的区间,满足以上要求。
来自百度。
答案2022-06-27:
这道题取巧了。用动态规划不是取巧的方式。
L0=最左0和最右0的长度,L1=最左1和最右1的长度,求L0和L1的最大值即可。
代码用rust编写。代码如下:
use rand::Rng;
use std::collections::HashMap;
fn main() {
let n: i32 = 500;
let test_time: i32 = 10000;
println!("测试开始");
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!("出错了!{}", i);
println!("ans1 = {}", ans1);
println!("ans2 = {}", ans2);
break;
}
}
println!("测试结束");
}
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);
}
// 为了测试
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;
}执行结果如下:
边栏推荐
- Applet: traverse the value of an array in the list, which is equivalent to for= "list" list An item in comment
- 我想网上注册股票开户,如何操作?在线开户安全么?
- 手机买同业存单基金开户选哪家证券公司比较好,比较安全呢
- Matlab tips (20) matrix analysis -- principal component regression
- Use of Jasper soft studio report tool and solution of thorny problems
- PMP考试重点总结七——监控过程组(1)
- HDI的盲孔设计,你注意到这个细节了吗?
- DEJA_ Vu3d - 051 of cesium function set - perfect realization of terrain excavation
- Divide and rule classic Hanoi
- [ybtoj advanced training guidance] class roll call [string hash]
猜你喜欢

1181:整数奇偶排序

The Cassandra cluster reinstalls and starts from the node. An error is reported. There is an existing solution

PMP考试重点总结八——监控过程组(2)

Dbeaver connects to kingbasees V8 (ultra detailed graphic tutorial)

This article explains in detail the difficult problems and solutions faced by 3D cameras

Rman Backup Report Ora - 19809 Ora - 19804
Understanding the IO model

虚拟机14安装win7(图教程)

Test cases for learning the basic content of software testing (II)

"Jianzhi offer" -- Interview Question 4: finding two-dimensional arrays
随机推荐
How do I open an account on my mobile phone? Is it safe to open an account online now?
Static page of pinyougou mall
详解final、finally和finalize
redis5.0的槽点迁移,随意玩(单机迁移集群)
Write a simple timeline
Which occupational groups are suitable for the examination
1. Kimball dimension modeling of data warehouse: what is a fact table?
Use of Jasper soft studio report tool and solution of thorny problems
Music website design based on harmonyos (portal page)
构造方法绝不是在new()之后就立马执行!!!!!
P2394 yyy loves Chemistry I
Play SFTP upload file
Import and export of a single collection in postman
玩玩sftp上传文件
new URL(“www.jjj.com“)
Which securities company is better and safer to choose when opening an account for the inter-bank certificate of deposit fund with mobile phone
Understanding the IO model
剑指Offer | 链表转置
为什么SELECT * 会导致查询效率低?
Data visualization makes correlation analysis easier to use