当前位置:网站首页>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;
}
执行结果如下:
边栏推荐
- Add token validation in swagger
- Amélioration de l'efficacité de la requête 10 fois! 3 solutions d'optimisation pour résoudre le problème de pagination profonde MySQL
- Solve the problem that the tabbar navigation at the bottom of vantui does not correspond to the page (window.loading.hash)
- 長文綜述:大腦中的熵、自由能、對稱性和動力學
- What is the difference between enterprise wechat applet and wechat applet
- Osnabrueck University | overview of specific architectures in the field of reinforcement learning
- CSP drawing
- warning: LF will be replaced by CRLF in XXXXXX
- logistic regression
- Easy to win insert sort
猜你喜欢
Unity controls the selection of the previous and next characters
Dare to climb here, you're not far from prison, reptile reverse actual combat case
JSON string conversion in unity
The difference between MCU serial communication and parallel communication and the understanding of UART
7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages
Is it really so difficult to learn redis? Today, a fan will share his personal learning materials!
New year's first race, submit bug reward more!
Teach you how to optimize SQL
The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
随机推荐
Dare to climb here, you're not far from prison, reptile reverse actual combat case
[latex] production of complex tables: excel2latex and detail adjustment
Basé sur... Netcore Development blog Project Starblog - (14) Implementation of theme switching function
Day05 錶格
Which product is better for 2022 annual gold insurance?
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
Short math guide for latex by Michael downs
There is no need to authorize the automatic dream weaving collection plug-in for dream weaving collection
Code Execution Vulnerability - no alphanumeric rce create_ function()
Redis transaction
Experience summary of the 12th Blue Bridge Cup (written for the first time)
【.NET+MQTT】.NET6 環境下實現MQTT通信,以及服務端、客戶端的雙邊消息訂閱與發布的代碼演示
2022 examination summary of quality controller - Equipment direction - general basis (quality controller) and examination questions and analysis of quality controller - Equipment direction - general b
Jenkins configures IP address access
Apple submitted the new MAC model to the regulatory database before the spring conference
查詢效率提昇10倍!3種優化方案,幫你解决MySQL深分頁問題
Keepalived set the master not to recapture the VIP after fault recovery (it is invalid to solve nopreempt)
2006 translation
[.NET + mqtt]. Mise en œuvre de la communication mqtt dans l'environnement net 6 et démonstration de code pour l'abonnement et la publication de messages bilatéraux du serveur et du client
Kiss number + close contact problem