当前位置:网站首页>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;
}
执行结果如下:
边栏推荐
- DataGuard active / standby cleanup archive settings
- Pypharm uses, and the third-party library has errors due to version problems
- [leetcode] 20. Valid brackets
- 【推荐系统基础】正负样本采样和构造
- Traduction gratuite en un clic de plus de 300 pages de documents PDF
- FFA and ICGA angiography
- Fully automated processing of monthly card shortage data and output of card shortage personnel information
- [question de programmation] [scratch niveau 2] oiseaux volants en décembre 2019
- 【編程題】【Scratch二級】2019.12 飛翔的小鳥
- Robomaster visual tutorial (10) target prediction
猜你喜欢
串联二极管,提高耐压
Problems faced when connecting to sqlserver after downloading (I)
Detailed explanation of interview questions: the history of blood and tears in implementing distributed locks with redis
Introduction to programming hardware
第四期SFO销毁,Starfish OS如何对SFO价值赋能?
Two small problems in creating user registration interface
Les mots ont été écrits, la fonction est vraiment puissante!
一键免费翻译300多页的pdf文档
Fully automated processing of monthly card shortage data and output of card shortage personnel information
At the age of 35, I made a decision to face unemployment
随机推荐
2022.7.7-----leetcode.648
C language 005: common examples
How to measure whether the product is "just needed, high frequency, pain points"
【编程题】【Scratch二级】2019.12 飞翔的小鸟
Data analysis series 3 σ Rule / eliminate outliers according to laida criterion
Magic fast power
Binary sort tree [BST] - create, find, delete, output
P1067 [noip2009 popularity group] polynomial output (difficult, pit)
正畸注意事项(持续更新中)
Solutions to problems in sqlserver deleting data in tables
2022.7.7-----leetcode. six hundred and forty-eight
QT creator add custom new file / Project Template Wizard
C - linear table
The result of innovation in professional courses such as robotics (Automation)
数据湖(十五):Spark与Iceberg整合写操作
[question de programmation] [scratch niveau 2] oiseaux volants en décembre 2019
Aitm3.0005 smoke toxicity test
面试题详解:用Redis实现分布式锁的血泪史
【leetcode】day1
Chisel tutorial - 02 Chisel environment configuration and implementation and testing of the first chisel module