当前位置:网站首页>2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
2022-06-24 10:05:00 【Fuda scaffold constructor's daily question】
2022-06-23: Given a nonnegative array , Select any number , Make the sum of accumulation maximum and equal to 7 Multiple , Returns the maximum cumulative sum .
n The larger ,10 Of 5 Power .
From meituan .3.26 written examination .
answer 2022-06-23:
want i Or not i, recursive . It can be changed into dynamic programming .
The code to use rust To write . The code is as follows :
use rand::Rng;
fn main() {
let len: i32 = 12;
let value: i32 = 100;
let test_time: i32 = 10000;
println!(" Beginning of the test ");
for i in 0..test_time {
let n = rand::thread_rng().gen_range(0, len) + 1;
let mut arr = random_array(n, value);
let ans1 = max_sum1(&mut arr);
let ans2 = max_sum2(&mut arr);
if ans1 != ans2 {
println!(" Something went wrong !{}", i);
println!("ans1 = {}", ans1);
println!("ans2 = {}", ans2);
break;
}
}
println!(" End of test ");
}
fn max_sum1(arr: &mut Vec<i32>) -> i32 {
return process1(arr, 0, 0);
}
fn process1(arr: &mut Vec<i32>, index: i32, pre: i32) -> i32 {
if index == arr.len() as i32 {
return if pre % 7 == 0 { pre } else { 0 };
}
let p1 = process1(arr, index + 1, pre);
let p2 = process1(arr, index + 1, pre + arr[index as usize]);
return get_max(p1, p2);
}
fn get_max<T: Clone + Copy + std::cmp::PartialOrd>(a: T, b: T) -> T {
if a > b {
a
} else {
b
}
}
fn max_sum2(arr: &mut Vec<i32>) -> i32 {
if arr.len() == 0 {
return 0;
}
let n = arr.len() as i32;
let mut dp: Vec<Vec<i32>> = vec![];
for i in 0..n {
dp.push(vec![]);
dp[i as usize].push(0);
for _ in 1..7 {
dp[i as usize].push(-1);
}
}
dp[0][(arr[0] % 7) as usize] = arr[0];
for i in 1..n {
// At present arr[i] % 7 The remainder of
let cur_mod = arr[i as usize] % 7;
for j in 0..7 {
dp[i as usize][j as usize] = dp[(i - 1) as usize][j as usize];
let find_mod = (7 - cur_mod + j) % 7;
if dp[(i - 1) as usize][find_mod as usize] != -1 {
dp[i as usize][j as usize] = get_max(
dp[i as usize][j as usize],
dp[(i - 1) as usize][find_mod as usize] + arr[i as usize],
);
}
}
}
return if dp[(n - 1) as usize][0] == -1 {
0
} else {
dp[(n - 1) as usize][0]
};
}
// In order to test
fn random_array(n: i32, v: i32) -> Vec<i32> {
let mut arr: Vec<i32> = vec![];
for _i in 0..n {
arr.push(rand::thread_rng().gen_range(0, v) + 1);
}
return arr;
}The results are as follows :
边栏推荐
- canvas管道动画js特效
- 微信小程序学习之 实现列表渲染和条件渲染.
- 2021-08-17
- How to make social media the driving force of cross-border e-commerce? This independent station tool cannot be missed!
- 针对《VPP实现策略路由》的修正
- Can the long-term financial products you buy be shortened?
- Tutorial (5.0) 08 Fortinet security architecture integration and fortixdr * fortiedr * Fortinet network security expert NSE 5
- [input method] so far, there are so many Chinese character input methods!
- Regular matching mobile number
- PostgreSQL DBA快速入门-通过源码编译安装
猜你喜欢

如何解决独立站多渠道客户沟通难题?这款跨境电商插件一定要知道!

Binary tree part I

Ora-28000 error after upgrading Oracle 12C to 19C

Use of vim

物联网?快来看 Arduino 上云啦

Thinkphp5 multi language switching project practice

JCIM|药物发现中基于AI的蛋白质结构预测:影响和挑战

nVisual数字基础设施运营管理软件平台

Nvisual digital infrastructure operation management software platform

Servlet快速筑基
随机推荐
Is there a reliable and low commission futures account opening channel in China? Is it safe to open an account online?
读取csv(tsv)文件出错
队列Queue
五心红娘
oracle池式连接请求超时问题排查步骤
桌面软件开发框架大赏
Implementation of simple floating frame in WindowManager
Binary tree part I
416 binary tree (first, middle and last order traversal iteration method)
Prct-1400: failed to execute getcrshome resolution
415 binary tree (144. preorder traversal of binary tree, 145. postorder traversal of binary tree, 94. inorder traversal of binary tree)
Cookie encryption 4 RPC method determines cookie encryption
如何提高网络基础设施排障效率,告别数据断档?
[Eureka source code analysis]
Thinkphp5 multi language switching project practice
算法---矩阵中战斗力最弱的 K 行(Kotlin)
记录一下MySql update会锁定哪些范围的数据
Array seamless scrolling demo
Groovy obtains Jenkins credentials through withcredentials
MYSQL数据高级