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

边栏推荐
- Defensive programming skills
- SQL injection (1) -- determine whether there are SQL injection vulnerabilities
- Easy to win insert sort
- 2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
- Zigzag scan
- 选择排序与冒泡排序模板
- what does ctrl + d do?
- The property of judging odd or even numbers about XOR.
- POSTECH | option compatible reward reverse reinforcement learning
- Leecode 122. Zuijia timing of buying and selling stocks ②
猜你喜欢

Setting methods, usage methods and common usage scenarios of environment variables in postman

SQL injection (1) -- determine whether there are SQL injection vulnerabilities

JSON string conversion in unity

Constantly changing harmonyos custom JS components during the Spring Festival - Smart Koi

MySQL is dirty

Session learning diary 1

Rhcsa day 2
![[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush](/img/94/2bdc31ec05595dbbc8a7a8d6b22252.jpg)
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush

How to use websocket to realize simple chat function in C #

Fudan released its first review paper on the construction and application of multimodal knowledge atlas, comprehensively describing the existing mmkg technology system and progress
随机推荐
Add IDM to Google browser
New year's first race, submit bug reward more!
Management and thesis of job management system based on SSM
SQL语句加强练习(MySQL8.0为例)
Webhook triggers Jenkins for sonar detection
渗透实战-guest账户-mimikatz-向日葵-sql提权-离线解密
Es network layer
CUDA basic knowledge
Why is it recommended that technologists write blogs?
选择排序与冒泡排序模板
Recursive structure
Base d'apprentissage de la machine: sélection de fonctionnalités avec lasso
JVM family -- heap analysis
2006 translation
Examination question bank of constructor decoration direction post skills (constructor) and examination data of constructor decoration direction post skills (constructor) in 2022
Jenkins continuous integration environment construction V (Jenkins common construction triggers)
[development team follows] API specification
How to use STR function of C language
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
Solve the problems encountered by the laravel framework using mongodb