当前位置:网站首页>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 :

边栏推荐
- The 37 year old programmer was laid off, and he didn't find a job for 120 days. He had no choice but to go to a small company. As a result, he was confused
- Contest3145 - the 37th game of 2021 freshman individual training match_ E: Eat watermelon
- 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
- 機器學習基礎:用 Lasso 做特征選擇
- [source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
- 深入浅出对话系统——使用Transformer进行文本分类
- JS object definition
- Www 2022 | taxoenrich: self supervised taxonomy complemented by Structural Semantics
- How to use STR function of C language
- Easy to win insert sort
猜你喜欢

Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website

Rhcsa day 3

Zhihu million hot discussion: why can we only rely on job hopping for salary increase? Bosses would rather hire outsiders with a high salary than get a raise?
![Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]](/img/0d/7a8370d153a8479b706377c3487220.jpg)
Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]

Development of digital collection trading platform development of digital collection platform
![Stm32bug [stlink forced update prompt appears in keilmdk, but it cannot be updated]](/img/ad/b675364fcaf5d874397fd0cbfec11b.jpg)
Stm32bug [stlink forced update prompt appears in keilmdk, but it cannot be updated]
![[Wu Enda deep learning] beginner learning record 3 (regularization / error reduction)](/img/e9/818bdfeae766dca7d2318b52b4424d.jpg)
[Wu Enda deep learning] beginner learning record 3 (regularization / error reduction)

Katalon框架测试web(二十六)自动发邮件

The 37 year old programmer was laid off, and he didn't find a job for 120 days. He had no choice but to go to a small company. As a result, he was confused

MySQL is dirty
随机推荐
New year's first race, submit bug reward more!
Webhook triggers Jenkins for sonar detection
Exercices de renforcement des déclarations SQL (MySQL 8.0 par exemple)
Why is it recommended that technologists write blogs?
what does ctrl + d do?
Setting methods, usage methods and common usage scenarios of environment variables in postman
What are the virtual machine software? What are their respective functions?
Www 2022 | taxoenrich: self supervised taxonomy complemented by Structural Semantics
GUI Graphical user interface programming (XIV) optionmenu - what do you want your girlfriend to wear on Valentine's day
The difference between MCU serial communication and parallel communication and the understanding of UART
Es network layer
渗透实战-SQLServer提权
Add IDM to Google browser
Osnabrueck University | overview of specific architectures in the field of reinforcement learning
Backpropagation formula derivation [Li Hongyi deep learning version]
What kind of experience is it when the Institute earns 20000 yuan a month!
Typical applications of minimum spanning tree
Base d'apprentissage de la machine: sélection de fonctionnalités avec lasso
基於.NetCore開發博客項目 StarBlog - (14) 實現主題切換功能
The 37 year old programmer was laid off, and he didn't find a job for 120 days. He had no choice but to go to a small company. As a result, he was confused