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

边栏推荐
- Solve the problem that the tabbar navigation at the bottom of vantui does not correspond to the page (window.loading.hash)
- Leecode 122. Zuijia timing of buying and selling stocks ②
- Contest3145 - the 37th game of 2021 freshman individual training match_ F: Smallest ball
- What kind of experience is it when the Institute earns 20000 yuan a month!
- [database I] database overview, common commands, view the table structure of 'demo data', simple query, condition query, sorting data, data processing function (single row processing function), groupi
- Redis notes (I) Linux installation process of redis
- MySQL query
- I stepped on a foundation pit today
- Www 2022 | taxoenrich: self supervised taxonomy complemented by Structural Semantics
- Which product is better for 2022 annual gold insurance?
猜你喜欢

Defensive programming skills

PHP database connection succeeded, but data cannot be inserted

150 ppt! The most complete "fair perception machine learning and data mining" tutorial, Dr. AIST Toshihiro kamishima, Japan

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

Unity knapsack system (code to center and exchange items)

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"

Redis transaction

National standard gb28181 protocol platform easygbs fails to start after replacing MySQL database. How to deal with it?

JVM family -- monitoring tools

What is the difference between enterprise wechat applet and wechat applet
随机推荐
Contest3145 - the 37th game of 2021 freshman individual training match_ 1: Origami
Is it really so difficult to learn redis? Today, a fan will share his personal learning materials!
Osnabrueck University | overview of specific architectures in the field of reinforcement learning
基於.NetCore開發博客項目 StarBlog - (14) 實現主題切換功能
Webhook triggers Jenkins for sonar detection
Teach you how to optimize SQL
PHP database connection succeeded, but data cannot be inserted
Formulaire day05
【.NET+MQTT】.NET6 環境下實現MQTT通信,以及服務端、客戶端的雙邊消息訂閱與發布的代碼演示
Constantly changing harmonyos custom JS components during the Spring Festival - Smart Koi
PMP 考試常見工具與技術點總結
Rhcsa day 3
Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website
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
(column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)
What are the conditions for the opening of Tiktok live broadcast preview?
If you have just joined a new company, don't be fired because of your mistakes
Learning video website
Contest3145 - the 37th game of 2021 freshman individual training match_ D: Ranking
Typical applications of minimum spanning tree