力扣375周赛

力扣第375场周赛

统计已测试设备

差分数组优化

cpp 复制代码
class Solution {
public:
    int countTestedDevices(vector<int> &batteryPercentages) {
        int dec = 0;
        for (int x : batteryPercentages) {
            dec += x > dec;
        }
        return dec;
    }
};

双模幂运算

快速幂模拟

cpp 复制代码
class Solution {
public:
    long long qmi(int a, int b, int p)// a^b % p
    {
        long long res = 1 % p;
        while (b)
        {
            if (b & 1) res = res * a % p;
            a = a * (long long)a % p;
            b >>= 1;
        }
        return res;
    }
    vector<int> getGoodIndices(vector<vector<int>>& variables, int target) {
        int n = variables.size();
        vector<int> ans;
        for(int i = 0 ; i < n ; i ++){
            int ai = variables[i][0] , bi = variables[i][1] , ci = variables[i][2] , mi = variables[i][3];
            if(qmi(qmi(ai , bi , 10) , ci , mi) == target)ans.push_back(i);
        }
        return ans;
    }
};

统计最大元素出现至少 K 次的子数组

滑窗统计K 次

cpp 复制代码
class Solution {
public:
    long long countSubarrays(vector<int>& nums, int k) {
        int n = nums.size() , maxn = -1 ;
        for(auto x : nums)maxn = max(maxn , x);
        unordered_map<int , int> m;
        long long ans = 0;
        for(int i = 0 , j = 0 ; i < n ; i ++){
            m[nums[i]] ++;
            while(m[maxn] >= k){
                m[nums[j++]] --;
            }
            ans += j;
        }
        return ans;
    }
};

统计好分割方案的数目

区间合并求个数

cpp 复制代码
class Solution {
public:
    int numberOfGoodPartitions(vector<int>& nums) {
        //找区间
        unordered_map<int,pair<int,int>> ps;
        for(int i = 0 ; i < nums.size() ; i ++){
            int x = nums[i];
            auto it = ps.find(x);
            if(it != ps.end()){
                it ->second.second = i;
            }else {
                ps[x] = {i , i};
            }
        }
        vector<pair<int,int>> a;
        for(auto &[_ , p] : ps){
            a.emplace_back(p);
        }
        //排序区间
        sort(a.begin() , a.end() , [](const auto &p , const auto &q){
            return p.first < q.first;
        });

        //区间合并
        int ans = 1;
        int max_r = a[0].second;
        for(int i = 1 ; i < a.size() ; i ++){
            int left = a[i].first , right = a[i].second;
            if(left > max_r){
                ans = ans * 2 % 1'000'000'007;
            }
            max_r = max(max_r, right);
        }
        return ans;
    }
};

--

相关推荐
森焱森31 分钟前
水下航行器外形分类详解
c语言·单片机·算法·架构·无人机
QuantumStack2 小时前
【C++ 真题】P1104 生日
开发语言·c++·算法
写个博客3 小时前
暑假算法日记第一天
算法
绿皮的猪猪侠3 小时前
算法笔记上机训练实战指南刷题
笔记·算法·pta·上机·浙大
hie988944 小时前
MATLAB锂离子电池伪二维(P2D)模型实现
人工智能·算法·matlab
杰克尼4 小时前
BM5 合并k个已排序的链表
数据结构·算法·链表
.30-06Springfield5 小时前
决策树(Decision tree)算法详解(ID3、C4.5、CART)
人工智能·python·算法·决策树·机器学习
我不是哆啦A梦5 小时前
破解风电运维“百模大战”困局,机械版ChatGPT诞生?
运维·人工智能·python·算法·chatgpt
xiaolang_8616_wjl5 小时前
c++文字游戏_闯关打怪
开发语言·数据结构·c++·算法·c++20
small_wh1te_coder5 小时前
硬件嵌入式学习路线大总结(一):C语言与linux。内功心法——从入门到精通,彻底打通你的任督二脉!
linux·c语言·汇编·嵌入式硬件·算法·c