力扣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;
    }
};

--

相关推荐
向阳@向远方21 分钟前
第二章 简单程序设计
开发语言·c++·算法
github_czy1 小时前
RRF (Reciprocal Rank Fusion) 排序算法详解
算法·排序算法
许愿与你永世安宁2 小时前
力扣343 整数拆分
数据结构·算法·leetcode
爱coding的橙子2 小时前
每日算法刷题Day42 7.5:leetcode前缀和3道题,用时2h
算法·leetcode·职场和发展
满分观察网友z2 小时前
从一次手滑,我洞悉了用户输入的所有可能性(3330. 找到初始输入字符串 I)
算法
YuTaoShao3 小时前
【LeetCode 热题 100】73. 矩阵置零——(解法二)空间复杂度 O(1)
java·算法·leetcode·矩阵
Heartoxx3 小时前
c语言-指针(数组)练习2
c语言·数据结构·算法
大熊背3 小时前
图像处理专业书籍以及网络资源总结
人工智能·算法·microsoft
满分观察网友z3 小时前
别怕树!一层一层剥开它的心:用BFS/DFS优雅计算层平均值(637. 二叉树的层平均值)
算法
杰克尼4 小时前
1. 两数之和 (leetcode)
数据结构·算法·leetcode