力扣375周赛

力扣第375场周赛

统计已测试设备

差分数组优化

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

双模幂运算

快速幂模拟

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 次

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

统计好分割方案的数目

区间合并求个数

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

相关推荐

  1. 375

    2023-12-11 10:08:02       36 阅读
  2. 376

    2023-12-11 10:08:02       38 阅读
  3. 379VP

    2023-12-11 10:08:02       36 阅读
  4. 381

    2023-12-11 10:08:02       37 阅读
  5. 382

    2023-12-11 10:08:02       20 阅读
  6. 119双

    2023-12-11 10:08:02       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-11 10:08:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-11 10:08:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-11 10:08:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-11 10:08:02       18 阅读

热门阅读

  1. Python高级算法——贪心算法(Greedy Algorithm)

    2023-12-11 10:08:02       36 阅读
  2. MySQL中的数据类型

    2023-12-11 10:08:02       34 阅读
  3. MapReduce

    2023-12-11 10:08:02       24 阅读
  4. 哈顿矩阵:预防伤害的项目管理技术

    2023-12-11 10:08:02       26 阅读
  5. vivado时序方法检查6

    2023-12-11 10:08:02       35 阅读
  6. 【数据结构】堆的应用(小根堆)

    2023-12-11 10:08:02       35 阅读
  7. BBS项目

    2023-12-11 10:08:02       39 阅读
  8. 机器人集群控制算法概述

    2023-12-11 10:08:02       31 阅读
  9. 实验五 Spring Boot项目开发

    2023-12-11 10:08:02       38 阅读