当前位置:网站首页>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;
}
执行结果如下:

边栏推荐
- Tsinghua University product: penalty gradient norm improves generalization of deep learning model
- Cache general management class + cache httpcontext Current. Cache and httpruntime Differences between caches
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- What are the virtual machine software? What are their respective functions?
- Examination question bank of constructor decoration direction post skills (constructor) and examination data of constructor decoration direction post skills (constructor) in 2022
- CSP drawing
- Recent learning fragmentation (14)
- The first spring of the new year | a full set of property management application templates are presented, and Bi construction is "out of the box"
- MySQL data query optimization -- data structure of index
- Which product is better if you want to go abroad to insure Xinguan?
猜你喜欢

Management and thesis of job management system based on SSM

No clue about the data analysis report? After reading this introduction of smartbi, you will understand!

150 ppt! The most complete "fair perception machine learning and data mining" tutorial, Dr. AIST Toshihiro kamishima, Japan
![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]

Consul of distributed service registration discovery and unified configuration management

Easy to win insert sort

Tsinghua University product: penalty gradient norm improves generalization of deep learning model

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

Dare to climb here, you're not far from prison, reptile reverse actual combat case

In my spare time, I like to write some technical blogs and read some useless books. If you want to read more of my original articles, you can follow my personal wechat official account up technology c
随机推荐
Site favorites
Solve the problems encountered by the laravel framework using mongodb
Contest3145 - the 37th game of 2021 freshman individual training match_ G: Score
MySQL backup notes
This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t
Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website
Defensive programming skills
長文綜述:大腦中的熵、自由能、對稱性和動力學
[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love
Want to do something in production? Then try these redis commands
PID of sunflower classic
Learning video website
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
CSCI 2134
The property of judging odd or even numbers about XOR.
Monitoring - Prometheus introduction
(practice C language every day) pointer sorting problem
Dare to climb here, you're not far from prison, reptile reverse actual combat case
Optimization theory: definition of convex function + generalized convex function
Enhanced for loop