当前位置:网站首页>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;
}
执行结果如下:
边栏推荐
- 具有CT造影功能的硫化铋纳米棒|硫化铋-锌原卟啉复合材料(PAMAM/Bi2S3复合纳米粒子)
- 四氧化三铁/硫化铋纳米复合材料([email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂
- 运放-运算放大器经典应用电路大全-应用电路大全
- 常用Oracle命令
- golang xml 处理动态属性
- db.sqlite3没有“as Data Source“解决方法
- 超顺磁四氧化三铁@二氧化硅@硫化镉纳米核壳结构材料|表面接枝mPEG的Fe3O4磁性纳米颗粒(f-Fe3O4)|相关产品
- CalBioreagents超全Id 蛋白兔单克隆抗体,助力科研
- 文本字符串长度排序 - 在线工具
- IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用
猜你喜欢
[email protected]@cadmium sulfide nanocore-shell structure material|Fe3O4 magnetic nanop"/>
Superparamagnetic iron [email protected]@cadmium sulfide nanocore-shell structure material|Fe3O4 magnetic nanop
sql问题解答创建表的语句
redis 运行lua 脚本 出现Invalid argument(s)
05 多线程与高并发 - ThreadPoolExecutor 源码解析
CMake中INSTALL_RPATH与BUILD_RPATH问题
phpstudy install flarum forum
线程的6种状态
MYSQLg高级------批量插入百万级数据量
ZIP压缩包文件删除密码的方法
深度学习-神经网络原理2
随机推荐
Ferric oxide/bismuth sulfide nanocomposites ([email protected]@BSABiS nanoparticles) | dendrimer-stabilized bismuth sulfide nanop
Adds, deletes, searches, and changes the leading doubly circular linked list (implemented in C language)
DevNet: Deviation Aware Networkfor Lane Detection
【Wwise】ArgumentException: The specified path is not of a legal form (empty).关于WwiseGlobal中的路径读取错误问题
kubernetes apparmor 简介
MYSQL Advanced Chapter - Query Interception Analysis, Lock Mechanism, Master-Slave Replication
qt send mail program
Unity backgammon game design and simple AI implementation (1)
uniapp实现防抖搜索
GNNExplainer应用于节点分类任务
像天才一样思考:如何培养自己的创造力?
Unity C# 委托——事件,Action,Func的作用和区别
MYSQL高级篇-----查询截取分析,锁机制,主从复制
static静态关键字和继承
05 多线程与高并发 - ThreadPoolExecutor 源码解析
Excel受保护的工作表怎么操作?
Qt 学习(三) —— Qt 模块
ZIP压缩包文件删除密码的方法
工控设备的系统如何进行加固
程序性能分析 —— 复杂度分析