当前位置:网站首页>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;
}
执行结果如下:

边栏推荐
- MYSQL Advanced Chapter - Query Interception Analysis, Lock Mechanism, Master-Slave Replication
- 报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disab
- BeautifulSoup4的介绍与使用
- db.sqlite3没有“as Data Source“解决方法
- golang zip aes base64
- 文本字符串长度排序 - 在线工具
- 报错jinja2.exceptions.UndefinedError: ‘form‘ is undefined
- 深度学习-神经网络原理2
- Fe3O4/SiO2 Composite Magnetic Nanoparticles Aminated on SiO2-NH2/Fe3O4 Surface (Qiyue Reagent)
- Unity backgammon game design and simple AI implementation (1)
猜你喜欢

中英文说明书丨CalBioreagents 山羊抗人白蛋白,IgG组分

Unity 五子棋游戏设计和简单AI(2)

sql problem solving statement to create a table

输入框最前面添加放大镜&&background-image和background-color冲突问题

中英文说明书丨TRC D-阿卓糖(D-Altrose)

报错jinja2.exceptions.UndefinedError: ‘form‘ is undefined
![[GO], arrays and slices](/img/71/86126c41d0f43aa8b9f232b219f5d7.png)
[GO], arrays and slices

超顺磁四氧化三铁@二氧化硅@硫化镉纳米核壳结构材料|表面接枝mPEG的Fe3O4磁性纳米颗粒(f-Fe3O4)|相关产品

kubernetes apparmor入门

Xilinx Zynq ZynqMP DNA
随机推荐
工控设备的系统如何进行加固
static静态关键字和继承
报错jinja2.exceptions.UndefinedError: ‘form‘ is undefined
[GO], arrays and slices
golang zip aes base64
【Wwise】ArgumentException: The specified path is not of a legal form (empty). About the path reading error in WwiseGlobal
BeautifulSoup4的介绍与使用
普罗米修斯原理及节点发布
【R语言】交互作用 测试数据
Unity backgammon game design and simple AI implementation (1)
Used to import the data type
C语言实现顺序栈和链队列
qt发送邮件程序
为什么以太网无法接收大于1500字节的数据包?
A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
Data center project preliminary summary
报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disab
zip压缩包密码解密
简单使用Lambda表达式
Deep Learning - Principles of Neural Networks 2