• LeetCode每日一题(2212. Maximum Points in an Archery Competition)


    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:

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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

    对于每一个 score section, bob 可以选择付出比 alice 多一根箭的代价来拿到这个分数, 或者直接略过这一个 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
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
  • 相关阅读:
    【枚举 + 求最大公约数方法】最大公约数等于K的子数组数目问题
    《Java并发编程的艺术》读书笔记 - 第八章 - Java中的并发工具类
    自定义注解
    PyTorch模型定义和相关使用
    【C++从0到王者】第三十三站:AVL树
    (27)Blender源码分析之顶层菜单的关于对话框
    技术分享 跨越语言的艺术:Flask Session 伪造
    SpringBoot进阶教程(七十五)数据脱敏
    【PTA-训练day4】L2-015 互评成绩 + L1-011 A-B
    ctfhub-文件上传-双写后缀
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/125466933