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

边栏推荐
- Apple submitted the new MAC model to the regulatory database before the spring conference
- Dare to climb here, you're not far from prison, reptile reverse actual combat case
- 2022 examination summary of quality controller - Equipment direction - general basis (quality controller) and examination questions and analysis of quality controller - Equipment direction - general b
- 数据库SQL语句汇总,持续更新......
- Objective C attribute keyword
- CUDA basic knowledge
- How to use websocket to realize simple chat function in C #
- Aperçu du code source futur - série juc
- Katalon框架测试web(二十六)自动发邮件
- Hospital network planning and design document based on GLBP protocol + application form + task statement + opening report + interim examination + literature review + PPT + weekly progress + network to
猜你喜欢

EV6 helps the product matrix, and Kia is making efforts in the high-end market. The global sales target in 2022 is 3.15 million?

@Scheduled scheduled tasks

Consul of distributed service registration discovery and unified configuration management

2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。

Recursive structure

Mindmanager2022 efficient and easy to use office mind map MindManager

What is cloud primordial?

The difference between MCU serial communication and parallel communication and the understanding of UART

Why is it recommended that technologists write blogs?

Setting methods, usage methods and common usage scenarios of environment variables in postman
随机推荐
Setting methods, usage methods and common usage scenarios of environment variables in postman
基于PHP的轻量企业销售管理系统
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
Constantly changing harmonyos custom JS components during the Spring Festival - Smart Koi
Rhcsa day 3
JS object definition
1289_FreeRTOS中vTaskSuspend()接口实现分析
Teach you how to optimize SQL
2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
[PaddleSeg 源码阅读] PaddleSeg Transform 的 Normalize操作
Sword finger offer:55 - I. depth of binary tree
Love and self-discipline and strive to live a core life
MySQL backup notes
Stm32bug [stlink forced update prompt appears in keilmdk, but it cannot be updated]
Wechat official account web page authorization
Imperial cms7.5 imitation "D9 download station" software application download website source code
Leecode 122. Zuijia timing of buying and selling stocks ②
Slurm view node configuration information
Osnabrueck University | overview of specific architectures in the field of reinforcement learning
POSTECH | option compatible reward reverse reinforcement learning