当前位置:网站首页>2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
2022-07-04 03:32:00 【福大大架构师每日一题】
2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。
请问翻转后可以使得1的个数最多是多少?
来自小红书。3.13笔试。
答案2022-07-03:
某个区间,0个数-1个数=最大值。
0变成1,1变成-1,求子数组的最大累加和。
代码用rust编写。代码如下:
use rand::Rng;
fn main() {
let nn: i32 = 100;
let test_time: i32 = 20000;
println!("测试开始");
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!("出错了!{}", i);
println!("ans1 = {}", ans1);
println!("ans2 = {}", ans2);
break;
}
}
println!("测试结束");
}
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;
}
// 为了测试
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;
}
执行结果如下:

边栏推荐
- 2006 translation
- JSON string conversion in unity
- Add token validation in swagger
- Eh, the log time of MySQL server is less than 8h?
- The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult
- Why is it recommended that technologists write blogs?
- Don't disagree, this is the most powerful "language" of the Internet
- logistic regression
- Which product is better if you want to go abroad to insure Xinguan?
- Aperçu du code source futur - série juc
猜你喜欢

Rhcsa day 2

96% of the collected traffic is prevented by bubble mart of cloud hosting

Easy to win insert sort

MySQL query

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

1day vulnerability pushback skills practice (3)

WordPress collection WordPress hang up collection plug-in
![[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love](/img/ab/066923f1aa1e8dd8dcc572cb60a25d.jpg)
[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love

Code Execution Vulnerability - no alphanumeric rce create_ function()

JVM family -- monitoring tools
随机推荐
How much does it cost to open a futures account in China? Where is it safe to open an account at present?
Contest3145 - the 37th game of 2021 freshman individual training match_ 1: Origami
false sharing
Osnabrueck University | overview of specific architectures in the field of reinforcement learning
Setting methods, usage methods and common usage scenarios of environment variables in postman
Is online futures account opening safe and reliable? Which domestic futures company is better?
Handler source code analysis
JSON string conversion in unity
The difference between MCU serial communication and parallel communication and the understanding of UART
What kind of experience is it when the Institute earns 20000 yuan a month!
【.NET+MQTT】.NET6 環境下實現MQTT通信,以及服務端、客戶端的雙邊消息訂閱與發布的代碼演示
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
I stepped on a foundation pit today
Learning video website
Management and thesis of job management system based on SSM
Contest3145 - the 37th game of 2021 freshman individual training match_ J: Eat radish
WP collection plug-in free WordPress collection hang up plug-in
CSP drawing
2022 registration examination for safety production management personnel of fireworks and firecracker production units and examination skills for safety production management personnel of fireworks an
Résumé: entropie, énergie libre, symétrie et dynamique dans le cerveau