第 402 场 LeetCode 周赛题解

A 构成整天的下标对数目 I

在这里插入图片描述

计数:遍历 h o u r s hours hours ,记录 h o u r s [ i ] % 24 hours[i]\%24 hours[i]%24 的出现次数

class Solution {
  public:
    long long countCompleteDayPairs(vector<int>& hours) {
        vector<int> cnt(24);
        long long res = 0;
        for (auto x : hours) {
            res += cnt[(24 - x % 24) % 24];
            cnt[x % 24]++;
        }
        return res;
    }
};

B 构成整天的下标对数目 II

在这里插入图片描述

计数:遍历 h o u r s hours hours ,记录 h o u r s [ i ] % 24 hours[i]\%24 hours[i]%24 的出现次数

class Solution {
  public:
    long long countCompleteDayPairs(vector<int>& hours) {
        vector<int> cnt(24);
        long long res = 0;
        for (auto x : hours) {
            res += cnt[(24 - x % 24) % 24];
            cnt[x % 24]++;
        }
        return res;
    }
};

C 施咒的最大总伤害

在这里插入图片描述

动态规划:设 p [ i ] p[i] p[i] 为最大伤害值不超过 i i i 的最大伤害值之和

class Solution {
  public:
    using ll = long long;
    long long maximumTotalDamage(vector<int>& power) {
        map<int, ll> s;
        for (auto x : power)
            s[x] += x;
        map<int, ll> p;
        for (auto it = s.begin(); it != s.end(); it++) {
            p[it->first] = it == s.begin() ? 0 : p[prev(it)->first];
            auto lb = s.lower_bound(it->first - 2);
            if (lb == s.begin())
                p[it->first] = max(p[it->first], it->second);
            else {
                p[it->first] = max(p[it->first], p[prev(lb)->first] + it->second);
            }
        }
        return p.rbegin()->second;
    }
};

D 数组中的峰值

在这里插入图片描述

树状数组:用树状数组维护前缀区间内的峰值元素数,对于更新 n u m s [ i n d e x ] nums[index] nums[index] 的操作,可能改变是否为峰值的位置有 i n d e x − 1 index-1 index1 i n d e x index index i n d e x + 1 index+1 index+1

class Solution {
  public:
    vector<int> countOfPeaks(vector<int>& nums, vector<vector<int>>& queries) {
        int n = nums.size();
        BinaryIndexedTree bit(n);
        auto isval = [](int i, int j, int k) { return j > i && j > k; };
        for (int i = 1; i < n - 1; i++)
            if (isval(nums[i - 1], nums[i], nums[i + 1]))
                bit.add(i + 1, 1);//初始化
        vector<int> res;
        for (auto& qi : queries) {
            if (qi[0] == 1) {
                if (qi[2] - qi[1] + 1 > 2)
                    res.push_back(bit.query(qi[2]) - bit.query(qi[1] + 1));
                else
                    res.push_back(0);
            } else { // update
                int i = qi[1];
                int val = qi[2];
                if (i - 1 > 0) {//判断nums[i-1]是否改变峰值性
                    if (!isval(nums[i - 2], nums[i - 1], nums[i]) && isval(nums[i - 2], nums[i - 1], val))
                        bit.add(i - 1 + 1, 1);
                    else if (isval(nums[i - 2], nums[i - 1], nums[i]) && !isval(nums[i - 2], nums[i - 1], val))
                        bit.add(i - 1 + 1, -1);
                }
                if (i + 1 < n - 1) {//判断nums[i+1]是否改变峰值性
                    if (!isval(nums[i], nums[i + 1], nums[i + 2]) && isval(val, nums[i + 1], nums[i + 2]))
                        bit.add(i + 1 + 1, 1);
                    else if (isval(nums[i], nums[i + 1], nums[i + 2]) && !isval(val, nums[i + 1], nums[i + 2]))
                        bit.add(i + 1 + 1, -1);
                }
                if (i != 0 && i != n - 1) {//判断nums[i]是否改变峰值性
                    if (!isval(nums[i - 1], nums[i], nums[i + 1]) && isval(nums[i - 1], val, nums[i + 1]))
                        bit.add(i + 1, 1);
                    if (isval(nums[i - 1], nums[i], nums[i + 1]) && !isval(nums[i - 1], val, nums[i + 1]))
                        bit.add(i + 1, -1);
                }
                nums[i] = val;
            }
        }
        return res;
    }

    class BinaryIndexedTree {//树状数组模板
      public:
        int N;
        vector<int> a;

        BinaryIndexedTree(int n) {
            N = n;
            a = vector<int>(N + 1);
        }

        inline int lowbit(int x) {
            return x & -x;
        }

        void add(int loc, int val) { // li[loc]+=val;
            for (; loc <= N; loc += lowbit(loc))
                a[loc] += val;
        }

        int query(int loc) { // sum{li[k] | 1<=k<=loc}
            int res = 0;
            for (; loc > 0; loc -= lowbit(loc))
                res += a[loc];
            return res;
        }
    };
};
  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值