当前位置:网站首页>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;
}
执行结果如下:
边栏推荐
- C语言实现顺序栈和链队列
- After the VB.net program is closed, the background is still connected to SQL
- VS2019常用快捷键
- Magnetic Core-Shell Fe3O4 Particles Supported Gold Nanostars | Magnetic Fe3O4-POSS-COOH | Superparamagnetic Fe3O4-Polydopamine Core-Shell Nanoparticles
- 声母-字母查询工具-词语缩写查询在线工具
- 【R语言】把文件夹下的所有文件提取到特定文件夹
- GNNExplainer applied to node classification task
- C# 利用iTextSharp画PDF
- 中英文说明书丨CalBioreagents 山羊抗人白蛋白,IgG组分
- 数据库中间件-jdbi
猜你喜欢
Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
PDF不能打印和复制的问题如何解决?
运放-运算放大器经典应用电路大全-应用电路大全
Introduction to AIOT
Magnetic Core-Shell Fe3O4 Particles Supported Gold Nanostars | Magnetic Fe3O4-POSS-COOH | Superparamagnetic Fe3O4-Polydopamine Core-Shell Nanoparticles
GNNExplainer应用于节点分类任务
【Feel】In the Unity Feel plugin, Camera cannot display CameraShake correctly
Unity 五子棋游戏设计和简单AI(2)
[GO]、数组与切片
sql problem solving statement to create a table
随机推荐
22年下高项论文题目预测
excel表格如何不需鼠标往下拖动而自动往下填
static静态关键字和继承
代码目录结构
mongo+ycsb性能测试及线程数分析
VB.net程序关闭后后台还在与SQL连接
电学知识的疑问
JMeter test - JMeter 】 【 upload multiple images/batch CSV file upload pictures interface parametric method
Text String Length Sorting - Online Tool
[R language] interaction test data
How to find package information and pin definitions for NXP S32K1xx series microcontrollers
中英文说明书丨TRC D-阿卓糖(D-Altrose)
Online tool for sorting multi-line strings
Redis 2 - 高级
Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
Ferric oxide/bismuth sulfide nanocomposites ([email protected]@BSABiS nanoparticles) | dendrimer-stabilized bismuth sulfide nanop
像天才一样思考:如何培养自己的创造力?
[R language] Normalize and organize files into folders of various file types
Excel受保护的工作表怎么操作?
Regular Expression - Determine if a string matches the "AABB" pattern