当前位置:网站首页>2022-08-08: Given an array arr, it represents the height of the missiles that will appear in order from morning to night.When the cannon shoots missiles, once the cannon is set to shoot at a certain h
2022-08-08: Given an array arr, it represents the height of the missiles that will appear in order from morning to night.When the cannon shoots missiles, once the cannon is set to shoot at a certain h
2022-08-09 06:26:00 【A question of the day for architects of Fuda University】
2022-08-08:给定一个数组arr,Indicates from morning to night,The heights of the missiles that will appear in turn.
When the cannon hits the missile,If once the cannon is set at a certain height to hit,Then the height of this cannon has to drop a little every time it hits.
(1) If only one cannon,Returns the maximum number of missiles that can be intercepted.
(2) If all missiles must be intercepted,Returns the minimum number of cannons.
答案2022-08-08:
问题一:最长递减子序列.There is just too much code on the web for the longest increasing subsequence,这里就不写了.
问题二:贪心+有序表.Hit it with the closest, slightly taller cannon that exists.
代码用rust编写.代码如下:
use std::collections::BTreeMap;
fn main() {
let mut arr = vec![15, 7, 14, 6, 5, 13, 5, 10, 9];
println!("ans = {}", num_of_cannon(&mut arr));
}
const MAX_VALUE: i32 = 1 << 31 - 1;
fn num_of_cannon(arr: &mut Vec<i32>) -> i32 {
// key : The ending value of a certain cannon shot
// value : How many cannons have the same ending value
// 比如:
// 一共有A、B、Cthree cannons
// 如果AThe height of the cannon at this time is 17,BThe height of the cannon at this time is 7,CThe height of the cannon at this time is 13
// then in the table:
// 7, 1
// 13, 1
// 17, 1
// 如果AThe height of the cannon at this time is 13,BThe height of the cannon at this time is 7,CThe height of the cannon at this time is 13
// then in the table:
// 7, 1
// 13, 2
let mut ends: BTreeMap<i32, i32> = BTreeMap::new();
for num in arr.iter() {
if ends.range(num + 1..).take(1).last() == None {
ends.insert(MAX_VALUE, 1);
}
let ceil_key = *ends.range(num + 1..).take(1).last().unwrap().0;
let ceil_value = *ends.range(num + 1..).take(1).last().unwrap().1;
if ceil_value > 1 {
ends.insert(ceil_key, ceil_value - 1);
} else {
ends.remove(&ceil_key);
}
ends.insert(
*num,
match ends.get(num) {
Option::Some(v) => v + 1,
Option::None => 1,
},
);
}
let mut ans = 0;
for (_, value) in ends.iter() {
ans += *value;
}
return ans;
}
执行结果如下:
边栏推荐
- 工控设备的系统如何进行加固
- sql问题解答创建表的语句
- [R language] Extract all files under a folder to a specific folder
- Likou Brush Question 180
- 推进产教融合 赋能教育创新发展 | 华云数据荣获“企业贡献奖”
- pycharm环境包导入到另外一个环境
- SiO2-NH2/Fe3O4表面氨基化的Fe3O4/SiO2复合磁性纳米粒子(齐岳试剂)
- 运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比
- 四氧化三铁/硫化铋纳米复合材料([email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂
- DDD 领域驱动设计
猜你喜欢
IQ Products CMV Brite Turbo试剂盒的原理
Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
SiO2/KH550修饰四氧化三铁纳米磁性颗粒|PDA包裹四氧化三铁磁性纳米颗粒(科研级)
Excel受保护的工作表怎么操作?
什么是excel文件保护
- [email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂"/>
四氧化三铁/硫化铋纳米复合材料([email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂
推进产教融合 赋能教育创新发展 | 华云数据荣获“企业贡献奖”
Gao Zelong, a famous digital collection expert and founder of the Digital Collection Conference, was interviewed by China Entrepreneur Magazine
sqlserver导入数据类型问题
Getting started with kubernetes apparmor
随机推荐
Deep Learning - Principles of Neural Networks 2
报错jinja2.exceptions.UndefinedError: ‘form‘ is undefined
运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比
【Wwise】ArgumentException: The specified path is not of a legal form (empty).关于WwiseGlobal中的路径读取错误问题
Introduction to AIOT
workbench 数据导出
简单使用Lambda表达式
22年下高项论文题目预测
JMeter test - JMeter 】 【 upload multiple images/batch CSV file upload pictures interface parametric method
pycharm环境包导入到另外一个环境
IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用
C语言实现顺序栈和链队列
[MySQL]二、进程的关系、MySQL密码破解、建表和建库相关命令
harbor企业级镜像仓库搭建
BeautifulSoup4的介绍与使用
A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
kubernetes apparmor 简介
【R语言】把文件夹下的所有文件提取到特定文件夹
安装flask
Word文件的只读模式没有密码怎么退出?