当前位置:网站首页>2022-07-07:原本数组中都是大于0、小于等于k的数字,是一个单调不减的数组, 其中可能有相等的数字,总体趋势是递增的。 但是其中有些位置的数被替换成了0,我们需要求出所有的把0替换的方案数量:
2022-07-07:原本数组中都是大于0、小于等于k的数字,是一个单调不减的数组, 其中可能有相等的数字,总体趋势是递增的。 但是其中有些位置的数被替换成了0,我们需要求出所有的把0替换的方案数量:
2022-07-07 22:01:00 【福大大架构师每日一题】
2022-07-07:原本数组中都是大于0、小于等于k的数字,是一个单调不减的数组,
其中可能有相等的数字,总体趋势是递增的。
但是其中有些位置的数被替换成了0,我们需要求出所有的把0替换的方案数量:
1)填充的每一个数可以大于等于前一个数,小于等于后一个数;
2)填充的每一个数不能大于k。
来自腾讯音乐。
答案2022-07-07:
方法一:动态规划。
方法二:数学方法。用到组合,C(b-a+m,m)。
代码用rust编写。代码如下:
use rand::Rng;
fn main() {
let nn: i64 = 20;
let kk: i64 = 30;
let test_time: i32 = 10000;
println!("测试开始");
for i in 0..test_time {
let n = rand::thread_rng().gen_range(0, nn) + 1;
let k = rand::thread_rng().gen_range(0, kk) + 1;
let mut arr = random_array(n, k);
let ans1 = ways1(&mut arr, k);
let ans2 = ways2(&mut arr, k);
if ans1 != ans2 {
println!("出错了!{}", i);
println!("ans1 = {}", ans1);
println!("ans2 = {}", ans2);
break;
}
}
println!("测试结束");
}
// 动态规划
fn ways1(nums: &mut Vec<i64>, k: i64) -> i64 {
let n = nums.len() as i64;
// dp[i][j] : 一共i个格子,随意填,但是不能降序,j种数可以选
let mut dp: Vec<Vec<i64>> = vec![];
for i in 0..n + 1 {
dp.push(vec![]);
for _ in 0..k + 1 {
dp[i as usize].push(0);
}
}
for i in 1..=n {
dp[i as usize][1] = 1;
}
for i in 1..=k {
dp[1][i as usize] = i;
}
for i in 2..=n {
for j in 2..=k {
dp[i as usize][j as usize] =
dp[(i - 1) as usize][j as usize] + dp[i as usize][(j - 1) as usize];
}
}
let mut res = 1;
let mut i: i64 = 0;
let mut j: i64 = 0;
while i < nums.len() as i64 {
if nums[i as usize] == 0 {
j = i + 1;
while j < nums.len() as i64 && nums[j as usize] == 0 {
j += 1;
}
let left_value = if i - 1 >= 0 {
nums[(i - 1) as usize]
} else {
1
};
let right_value = if j < nums.len() as i64 {
nums[j as usize]
} else {
k
};
res *= dp[(j - i) as usize][(right_value - left_value + 1) as usize];
i = j;
}
i += 1;
}
return res;
}
// 数学方法
// a ~ b范围的数字随便选,可以选重复的数,一共选m个
// 选出有序序列的方案数:C ( m, b - a + m )
fn ways2(nums: &mut Vec<i64>, k: i64) -> i64 {
let mut res = 1;
let mut i: i64 = 0;
let mut j: i64 = 0;
while i < nums.len() as i64 {
if nums[i as usize] == 0 {
j = i + 1;
while j < nums.len() as i64 && nums[j as usize] == 0 {
j += 1;
}
let left_value = if i - 1 >= 0 {
nums[(i - 1) as usize]
} else {
1
};
let right_value = if j < nums.len() as i64 {
nums[j as usize]
} else {
k
};
let numbers = j - i;
res *= c(right_value - left_value + numbers, numbers);
i = j;
}
i += 1;
}
return res;
}
// 从一共a个数里,选b个数,方法数是多少
fn c(a: i64, b: i64) -> i64 {
if a == b {
return 1;
}
let mut x = 1;
let mut y = 1;
let mut i = b + 1;
let mut j = 1;
while i <= a {
x *= i;
y *= j;
let gcd = gcd(x, y);
x /= gcd;
y /= gcd;
i += 1;
j += 1;
}
return x / y;
}
fn gcd(m: i64, n: i64) -> i64 {
if n == 0 {
m
} else {
gcd(n, m % n)
}
}
// 为了测试
fn random_array(n: i64, k: i64) -> Vec<i64> {
let mut ans: Vec<i64> = vec![];
for _i in 0..n {
ans.push(rand::thread_rng().gen_range(0, k) + 1);
}
ans.sort();
for i in 0..n {
ans[i as usize] = if rand::thread_rng().gen_range(0, 2) == 0 {
0
} else {
ans[i as usize]
};
}
return ans;
}
执行结果如下:
边栏推荐
- Kubectl 好用的命令行工具:oh-my-zsh 技巧和窍门
- 快速回复二极管整流特性
- 面试题详解:用Redis实现分布式锁的血泪史
- 一鍵免費翻譯300多頁的pdf文檔
- 全自动化处理每月缺卡数据,输出缺卡人员信息
- Aitm3.0005 smoke toxicity test
- HDU - 1260 tickets (linear DP)
- C language learning
- Fully automated processing of monthly card shortage data and output of card shortage personnel information
- One click free translation of more than 300 pages of PDF documents
猜你喜欢
Is 35 really a career crisis? No, my skills are accumulating, and the more I eat, the better
ROS from entry to mastery (IX) initial experience of visual simulation: turtlebot3
[path planning] use the vertical distance limit method and Bessel to optimize the path of a star
Robomaster visual tutorial (1) camera
Connect diodes in series to improve voltage withstand
Pycharm basic settings latest version 2022
Go learning notes (2) basic types and statements (1)
【编程题】【Scratch二级】2019.03 绘制方形螺旋
Benchmarking Detection Transfer Learning with Vision Transformers(2021-11)
CoinDesk评波场去中心化进程:让人们看到互联网的未来
随机推荐
用語雀寫文章了,功能真心强大!
Chisel tutorial - 05 Sequential logic in chisel (including explicit multi clock, explicit synchronous reset and explicit asynchronous reset)
Binary sort tree [BST] - create, find, delete, output
Two small problems in creating user registration interface
Robomaster visual tutorial (1) camera
ROS from entry to mastery (IX) initial experience of visual simulation: turtlebot3
正畸注意事项(持续更新中)
一个测试工程师的7年感悟 ---- 致在一路独行的你(别放弃)
QT and OpenGL: loading 3D models using the open asset import library (assimp) - Part 2
35岁真就成了职业危机?不,我的技术在积累,我还越吃越香了
2022.7.7-----leetcode. six hundred and forty-eight
Apng2gif solutions to various problems
mysql8.0 ubuntu20.4
数据湖(十五):Spark与Iceberg整合写操作
Restricted linear table
When creating body middleware, express Is there any difference between setting extended to true and false in urlencoded?
Trust orbtk development issues 2022
Alibaba cloud MySQL cannot connect
BSS 7230 flame retardant performance test of aviation interior materials
Handwriting a simulated reentrantlock