当前位置:网站首页>2022-07-03: there are 0 and 1 in the array. Be sure to flip an interval. Flip: 0 becomes 1, 1 becomes 0. What is the maximum number of 1 after turning? From little red book. 3.13 written examination.
2022-07-03: there are 0 and 1 in the array. Be sure to flip an interval. Flip: 0 becomes 1, 1 becomes 0. What is the maximum number of 1 after turning? From little red book. 3.13 written examination.
2022-07-04 03:37:00 【Fuda frame constructor daily question】
2022-07-03: In the array are 0 and 1, Be sure to flip an interval , Flip :0 change 1,1 change 0.
Can you turn it over to make 1 What is the maximum number of ?
From little red book .3.13 written examination .
answer 2022-07-03:
Some interval ,0 Number -1 Number = Maximum .
0 become 1,1 become -1, Find the maximum cumulative sum of subarrays .
The code to use rust To write . The code is as follows :
use rand::Rng;
fn main() {
let nn: i32 = 100;
let test_time: i32 = 20000;
println!(" Beginning of the test ");
for i in 0..test_time {
let n = rand::thread_rng().gen_range(0, nn) + 1;
let mut arr = random_array(n);
let mut arr2 = arr.clone();
let ans1 = max_one_numbers1(&mut arr);
let ans2 = max_one_numbers2(&mut arr2);
if ans1 != ans2 {
println!(" Something went wrong !{}", i);
println!("ans1 = {}", ans1);
println!("ans2 = {}", ans2);
break;
}
}
println!(" End of test ");
}
fn max_one_numbers1(arr: &mut Vec<i32>) -> i32 {
let mut ans = 0;
for l in 0..arr.len() as i32 {
for r in l..arr.len() as i32 {
reverse(arr, l, r);
ans = get_max(ans, one_numbers(arr));
reverse(arr, l, r);
}
}
return ans;
}
fn reverse(arr: &mut Vec<i32>, l: i32, r: i32) {
for i in l..=r {
arr[i as usize] ^= 1;
}
}
fn one_numbers(arr: &mut Vec<i32>) -> i32 {
let mut ans = 0;
for num in arr.iter() {
ans += *num;
}
return ans;
}
fn get_max<T: Clone + Copy + std::cmp::PartialOrd>(a: T, b: T) -> T {
if a > b {
a
} else {
b
}
}
fn max_one_numbers2(arr: &mut Vec<i32>) -> i32 {
let mut ans = 0;
for num in arr.iter() {
ans += *num;
}
for i in 0..arr.len() as i32 {
arr[i as usize] = if arr[i as usize] == 0 {
1 } else {
-1 };
}
let mut max = -2147483648;
let mut cur = 0;
for i in 0..arr.len() as i32 {
cur += arr[i as usize];
max = get_max(max, cur);
cur = if cur < 0 {
0 } else {
cur };
}
return ans + max;
}
// In order to test
fn random_array(n: i32) -> Vec<i32> {
let mut arr: Vec<i32> = vec![];
for _i in 0..n {
arr.push(rand::thread_rng().gen_range(0, 2));
}
return arr;
}
The results are as follows :
边栏推荐
- [.NET + mqtt]. Mise en œuvre de la communication mqtt dans l'environnement net 6 et démonstration de code pour l'abonnement et la publication de messages bilatéraux du serveur et du client
- POSTECH | option compatible reward reverse reinforcement learning
- Recent learning fragmentation (14)
- Exercices de renforcement des déclarations SQL (MySQL 8.0 par exemple)
- Why is it recommended that technologists write blogs?
- 数据库SQL语句汇总,持续更新......
- Want to do something in production? Then try these redis commands
- Calculate the odd sum of 1~n (1~100 as an example)
- 1day vulnerability pushback skills practice (3)
- Jenkins continuous integration environment construction V (Jenkins common construction triggers)
猜你喜欢
选择排序与冒泡排序模板
Mindmanager2022 efficient and easy to use office mind map MindManager
Third party login initial version
Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website
Leetcode51.n queen
What is cloud primordial?
Setting methods, usage methods and common usage scenarios of environment variables in postman
[untitled]
Have you entered the workplace since the first 00???
Don't disagree, this is the most powerful "language" of the Internet
随机推荐
Zlmediakit compilation and webrtc push-pull flow testing
Day05 錶格
Imperial cms7.5 imitation "D9 download station" software application download website source code
Apple submitted the new MAC model to the regulatory database before the spring conference
[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love
Defensive programming skills
How about the ratings of 2022 Spring Festival Gala in all provinces? Map analysis helps you show clearly!
[.NET + mqtt]. Mise en œuvre de la communication mqtt dans l'environnement net 6 et démonstration de code pour l'abonnement et la publication de messages bilatéraux du serveur et du client
Contest3145 - the 37th game of 2021 freshman individual training match_ 1: Origami
The difference between MCU serial communication and parallel communication and the understanding of UART
2022 attached lifting scaffold worker (special type of construction work) free test questions and attached lifting scaffold worker (special type of construction work) examination papers 2022 attached
Zigzag scan
渗透实战-guest账户-mimikatz-向日葵-sql提权-离线解密
Formulaire day05
Recursive structure
system information
Third party login initial version
GUI Graphical user interface programming (XIV) optionmenu - what do you want your girlfriend to wear on Valentine's day
Jenkins continuous integration environment construction V (Jenkins common construction triggers)
Examination question bank of constructor decoration direction post skills (constructor) and examination data of constructor decoration direction post skills (constructor) in 2022