第 375 场 LeetCode 周赛题解

news/2024/5/20 9:45:20 标签: leetcode, 算法, 快速幂, 滑动窗口, 计数, 动态规划

A leetcode.cn/problems/count-tested-devices-after-test-operations" rel="nofollow">统计已测试设备

在这里插入图片描述
在这里插入图片描述

模拟:记录当前已测试设备数量

class Solution {
public:
    int countTestedDevices(vector<int> &batteryPercentages) {
        int res = 0;
        int s = 0;
        for (auto x: batteryPercentages) {
            if (x - s > 0) {
                res++;
                s++;
            }
        }
        return res;
    }
};

B leetcode.cn/problems/double-modular-exponentiation" rel="nofollow">双模幂运算

在这里插入图片描述

快速幂

class Solution {
public:
    int fpow(int x, int n, int mod) {// x^n%mod
        int res = 1;
        for (int e = x; n; e = e * e % mod, n >>= 1)
            if (n & 1)
                res = res * e % mod;
        return res;
    }

    vector<int> getGoodIndices(vector<vector<int>> &variables, int target) {
        vector<int> res;
        for (int i = 0; i < variables.size(); i++)
            if (fpow(fpow(variables[i][0], variables[i][1], 10), variables[i][2], variables[i][3]) == target)
                res.push_back(i);
        return res;
    }
};

C leetcode.cn/problems/count-subarrays-where-max-element-appears-at-least-k-times" rel="nofollow">统计最大元素出现至少 K 次的子数组

在这里插入图片描述

滑动窗口:枚举滑窗的左边界 l l l,找到满足条件的最小右边界 r r r ,则左边界为 l l l 的满足条件的子数组数目为 n u m s . s i z e ( ) − r nums.size()-r nums.size()r

class Solution {
public:
    long long countSubarrays(vector<int> &nums, int k) {
        int mx = *max_element(nums.begin(), nums.end());
        int n = nums.size();
        long long res = 0;
        int cnt = 0;
        for (int l = 0, r = -1; l < n;) {
            while (cnt < k && r + 1 < n)
                if (nums[++r] == mx)
                    cnt++;
            if (cnt < k)
                break;
            res += n - r;
            if (nums[l++] == mx)
                cnt--;
        }
        return res;
    }
};

D leetcode.cn/problems/count-the-number-of-good-partitions" rel="nofollow">统计好分割方案的数目

在这里插入图片描述

计数:因为相同数字必须在一个子数组中,所以可以预先求出 n u m s nums nums 按要求最多可以划分成的子数组数目 c n t cnt cnt,原数组好分割方案数目等于 c n t cnt cnt 个互不相同的数形成的数组的好分割方案数目,即 2 c n t − 1 2^{cnt-1} 2cnt1

class Solution {
public:
    int numberOfGoodPartitions(vector<int> &nums) {
        int n = nums.size();
        unordered_map<int, int> r;//记录每个数出现的最右小标
        for (int i = 0; i < n; i++)
            r[nums[i]] = i;
        int cnt = 0;//按要求最多可以划分成的子数组数目
        for (int i = 0, right = r[nums[0]]; i < n;) {
            if (i != right) {
                right = max(right, r[nums[i++]]);
            } else {
                cnt++;
                if (i == n - 1)
                    break;
                right = r[nums[++i]];
            }
        }
        int mod = 1e9 + 7;
        int res = 1;
        for (int i = 0; i < cnt - 1; i++)
            res = res * 2 % mod;
        return (res + mod) % mod;
    }
};


http://www.niftyadmin.cn/n/5254446.html

相关文章

【已解决】ModuleNotFoundError: No module named ‘pandas‘

问题描述 ModuleNotFoundError: No module named ‘pandas’ 解决办法 pip install pandas 完结撒花 熟悉的人相遇&#xff0c;就像久旱等到的甘霖

google bigquery如何查询以太坊ethereum数据 sql怎么写

文档介绍 https://console.cloud.google.com/marketplace/details/ethereum/crypto-ethereum-blockchain?projectaqueous-tesla-294801 如查询 What are the 10 most popular Ethereum collectibles (ERC721 contracts), by number of transactions? SELECT contracts.addr…

node14升级node16之后,webpack3项目无法启动处理

node从14升级到16之后&#xff0c;项目就无法启动了&#xff0c;研究了webpack3升级5&#xff0c;研究好几个小时都无法启动&#xff0c;最后发现&#xff0c;微微升级几个版本就可以了。webpack还是3 版本改了好多个的&#xff0c;但是不确定具体是哪几个起作用的&#xff0c;…

Elasticsearch:向量数据库的真相

通过工作示例了解什么是向量数据库、它们如何实现 “相似性” 搜索以及它们可以在明显的 LLM 空间之外的哪些地方使用。除非你一直生活在岩石下&#xff0c;否则你可能听说过诸如生成式人工智能和大型语言模型&#xff08;LLM&#xff09;之类的术语。 除此之外&#xff0c;你很…

多维时序 | MATLAB实现RIME-CNN-BiLSTM-Multihead-Attention多头注意力机制多变量时间序列预测

多维时序 | MATLAB实现RIME-CNN-BiLSTM-Multihead-Attention多头注意力机制多变量时间序列预测 目录 多维时序 | MATLAB实现RIME-CNN-BiLSTM-Multihead-Attention多头注意力机制多变量时间序列预测预测效果基本介绍模型描述程序设计参考资料 预测效果 基本介绍 MATLAB实现RIME-…

【JavaFX】实现画图操作小程序

JavaFX提供了一个Canvas类来实现画图操作。下面是一个简单的例子,演示如何使用JavaFX绘制一个矩形: 创建一个JavaFX应用程序,添加一个场景和一个画布。public class Main extends Application {@Overridepublic void start(Stage primaryStage) {Group root = new Group();S…

一个不上进的爱好,让我走进了计算机世界

为什么当初选择计算机行业 当初选择计算机专业&#xff0c;真的就是觉得学习计算机专业&#xff0c;就可以经常接触计算机&#xff0c;可以有很多的机会可以玩游戏。 后来高考的时候&#xff0c;考试成绩也不理想&#xff0c;分数就不好意思说了。但是喜从天降&#xff0c;居…

算法基础十

加一 给定一个由 整数 组成的 非空 数组所表示的非负整数&#xff0c;在该数的基础上加一。最高位数字存放在数组的首位&#xff0c; 数组中每个元素只存储单个数字。 示例 1&#xff1a; 输入&#xff1a;digits [1,2,3] 输出&#xff1a;[1,2,4] 解释&#xff1a;输入数组表…