当前位置:网站首页>Leetcode daily question (2212. maximum points in an archery competition)
Leetcode daily question (2212. maximum points in an archery competition)
2022-07-03 09:33:00 【wangjun861205】
Alice and Bob are opponents in an archery competition. The competition has set the following rules:
Alice first shoots numArrows arrows and then Bob shoots numArrows arrows.
The points are then calculated as follows:
The target has integer scoring sections ranging from 0 to 11 inclusive.
For each section of the target with score k (in between 0 to 11), say Alice and Bob have shot ak and bk arrows on that section respectively. If ak >= bk, then Alice takes k points. If ak < bk, then Bob takes k points.
However, if ak == bk == 0, then nobody takes k points.
For example, if Alice and Bob both shot 2 arrows on the section with score 11, then Alice takes 11 points. On the other hand, if Alice shot 0 arrows on the section with score 11 and Bob shot 2 arrows on that same section, then Bob takes 11 points.
You are given the integer numArrows and an integer array aliceArrows of size 12, which represents the number of arrows Alice shot on each scoring section from 0 to 11. Now, Bob wants to maximize the total number of points he can obtain.
Return the array bobArrows which represents the number of arrows Bob shot on each scoring section from 0 to 11. The sum of the values in bobArrows should equal numArrows.
If there are multiple ways for Bob to earn the maximum total points, return any one of them.
Example 1:
Input: numArrows = 9, aliceArrows = [1,1,0,1,0,0,2,1,0,1,2,0]
Output: [0,0,0,0,1,1,0,0,1,2,3,1]
Explanation: The table above shows how the competition is scored.
Bob earns a total point of 4 + 5 + 8 + 9 + 10 + 11 = 47.
It can be shown that Bob cannot obtain a score higher than 47 points.
Example 2:
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-NXbpXyi5-1656207148005)(https://assets.leetcode.com/uploads/2022/02/24/ex2new.jpg)]
Input: numArrows = 3, aliceArrows = [0,0,1,0,0,0,0,0,0,0,0,2]
Output: [0,0,0,0,0,0,0,0,1,1,1,0]
Explanation: The table above shows how the competition is scored.
Bob earns a total point of 8 + 9 + 10 = 27.
It can be shown that Bob cannot obtain a score higher than 27 points.
Constraints:
- 1 <= numArrows <= 105
- aliceArrows.length == bobArrows.length == 12
- 0 <= aliceArrows[i], bobArrows[i] <= numArrows
- sum(aliceArrows[i]) == numArrows
For each of these score section, bob You can choose the pay ratio alice The price of one more arrow to get this score , Or skip this one directly score section
use std::collections::HashMap;
impl Solution {
fn dp(
num_arrows: i32,
alice_arrows: &Vec<i32>,
score_level: usize,
cache: &mut HashMap<(usize, i32), (Vec<i32>, i32)>,
) -> (Vec<i32>, i32) {
let alice = alice_arrows[score_level];
if score_level == 11 {
return (
vec![num_arrows],
if num_arrows <= alice {
0
} else {
score_level as i32
},
);
}
if num_arrows <= alice {
let (mut next_state, next_score) = if let Some((l, s)) =
cache.get(&(score_level + 1, num_arrows))
{
let state = l.clone();
(state.clone(), *s)
} else {
let (state, score) = Solution::dp(num_arrows, alice_arrows, score_level + 1, cache);
(state, score)
};
next_state.insert(0, 0);
cache.insert((score_level, num_arrows), (next_state.clone(), next_score));
return (next_state, next_score);
}
let (take_state, take_score) = if let Some((next_state, next_score)) =
cache.get(&(score_level + 1, num_arrows - alice - 1))
{
let mut state = next_state.clone();
state.insert(0, alice + 1);
(state, *next_score + score_level as i32)
} else {
let (mut next_state, mut next_score) =
Solution::dp(num_arrows - alice - 1, alice_arrows, score_level + 1, cache);
next_state.insert(0, alice + 1);
next_score += score_level as i32;
(next_state, next_score)
};
let (pass_state, pass_score) =
if let Some((next_state, next_score)) = cache.get(&(score_level + 1, num_arrows)) {
let mut next_state = next_state.clone();
next_state.insert(0, 0);
(next_state, *next_score)
} else {
let (mut next_state, score) =
Solution::dp(num_arrows, alice_arrows, score_level + 1, cache);
next_state.insert(0, 0);
(next_state, score)
};
if take_score > pass_score {
cache.insert((score_level, num_arrows), (take_state.clone(), take_score));
return (take_state, take_score);
}
cache.insert((score_level, num_arrows), (pass_state.clone(), pass_score));
(pass_state, pass_score)
}
pub fn maximum_bob_points(num_arrows: i32, alice_arrows: Vec<i32>) -> Vec<i32> {
let (state, _) = Solution::dp(num_arrows, &alice_arrows, 0, &mut HashMap::new());
state
}
}
边栏推荐
- 【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
- [CSDN]C1训练题解析_第三部分_JS基础
- The server denied password root remote connection access
- Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
- Matlab dichotomy to find the optimal solution
- Hudi data management and storage overview
- Patent inquiry website
- Jetson Nano 自定义启动图标kernel Logo cboot logo
- [CSDN] C1 training problem analysis_ Part III_ JS Foundation
- IDEA 中使用 Hudi
猜你喜欢
Global KYC service provider advance AI in vivo detection products have passed ISO international safety certification, and the product capability has reached a new level
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
LeetCode每日一题(2212. Maximum Points in an Archery Competition)
Spark 集群安装与部署
数字身份验证服务商ADVANCE.AI顺利加入深跨协 推进跨境电商行业可持续性发展
一款开源的Markdown转富文本编辑器的实现原理剖析
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks
【Kotlin学习】类、对象和接口——定义类继承结构
Win10 quick screenshot
CATIA automation object architecture - detailed explanation of application objects (I) document/settingcontrollers
随机推荐
LeetCode每日一题(931. Minimum Falling Path Sum)
Common formulas of probability theory
[kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions
文件系统中的目录与切换操作
Liteide is easy to use
Flink学习笔记(十一)Table API 和 SQL
全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶
WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
Flask+supervisor installation realizes background process resident
IDEA 中使用 Hudi
Temper cattle ranking problem
LeetCode每日一题(2115. Find All Possible Recipes from Given Supplies)
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
The idea of compiling VBA Encyclopedia
Notes on numerical analysis (II): numerical solution of linear equations
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
Failed building wheel for argon2 cffi when installing Jupiter
[CSDN] C1 training problem analysis_ Part III_ JS Foundation
PowerDesigner does not display table fields, only displays table names and references, which can be modified synchronously
Powerdesign reverse wizard such as SQL and generates name and comment