Leetcode 第 416 场周赛题解

Leetcode 第 416 场周赛题解

题目1:3295. 举报垃圾信息

思路

将字符串数组 bannedWords 的字符串保存在一个哈希表里。

遍历字符串数组 message 的每一个字符串 mes,如果 mes 在哈希表中出现,count++。

如果 count >= 2,说明数组 message 是垃圾信息,则返回 true;否则返回 false。

代码

/*
 * @lc app=leetcode.cn id=3295 lang=cpp
 *
 * [3295] 举报垃圾信息
 */

// @lc code=start
class Solution
{
public:
    bool reportSpam(vector<string> &message, vector<string> &bannedWords)
    {
        unordered_set<string> hashSet(bannedWords.begin(), bannedWords.end());
        int count = 0;
        for (string &mes : message)
            if (hashSet.contains(mes))
                count++;
        return count >= 2;
    }
};
// @lc code=end

复杂度分析

时间复杂度:O((n+m) * l),其中 n 是数组 message 的长度,m 是数组 bannedWords 的长度,l≤15 是字符串的最大长度。

空间复杂度:O(m * l),其中 m 是数组 bannedWords 的长度,l≤15 是字符串的最大长度。

题目2:3296. 移山所需的最少秒数

思路

最小堆模拟。

循环 mountainHeight 次,每次选一个「工作后总用时」最短的工人,把山的高度降低 1。

代码

/*
 * @lc app=leetcode.cn id=3296 lang=cpp
 *
 * [3296] 移山所需的最少秒数
 */

// @lc code=start
class Solution
{
public:
    long long minNumberOfSeconds(int mountainHeight, vector<int> &workerTimes)
    {
        priority_queue<tuple<long long, int, int>, vector<tuple<long long, int, int>>, greater<>> pq;
        for (int i = 0; i < workerTimes.size(); i++)
            pq.push(tuple<long long, int, int>(workerTimes[i], 1, i));

        long long ans = 0LL;
        while (mountainHeight--)
        {
            auto [cost, cnt, idx] = pq.top();
            pq.pop();
            ans = cost;

            cnt++;
            cost += (long long)cnt * workerTimes[idx];
            pq.push(tuple<long long, int, int>(cost, cnt, idx));
        }
        return ans;
    }
};
// @lc code=end

复杂度分析

时间复杂度:O(mountainHeight * logn),其中 n 是数组 workerTimes 的长度。

空间复杂度:O(n),其中 n 是数组 workerTimes 的长度。

题目3:3297. 统计重新排列后包含另一个字符串的子字符串数目 I

思路

哈希表 + 滑动窗口。

我们要求 word1 中的窗口中的对应字母的数量要大于等于 word2 中的数量,满足条件后,然后其到末尾会有 len1-right+1 种。

代码

/*
 * @lc app=leetcode.cn id=3297 lang=cpp
 *
 * [3297] 统计重新排列后包含另一个字符串的子字符串数目 I
 */

// @lc code=start
class Solution
{
public:
    long long validSubstringCount(string word1, string word2)
    {
        vector<int> cnt(26, 0);
        for (char &c : word2)
            cnt[c - 'a']--;
        int shortage = 0;
        for (int i = 0; i < 26; i++)
            if (cnt[i] < 0)
                shortage++;

        int len1 = word1.size();
        long long ans = 0LL;
        for (int left = 0, right = 0; left < len1; left++)
        {
            while (right < len1 && shortage > 0)
            {
                cnt[word1[right] - 'a']++;
                if (cnt[word1[right] - 'a'] == 0)
                    shortage--;
                right++;
            }

            if (shortage == 0)
                ans += len1 - right + 1;

            cnt[word1[left] - 'a']--;
            if (cnt[word1[left] - 'a'] == -1)
                shortage++;
        }

        return ans;
    }
};
// @lc code=end

复杂度分析

时间复杂度:O(n+m+∣Σ∣),其中 n 是字符串 word1 的长度,m 是字符串 word2 的长度,∣Σ∣=26 是字符集大小。

空间复杂度:O(∣Σ∣),其中 ∣Σ∣=26 是字符集大小。

题目4:3298. 统计重新排列后包含另一个字符串的子字符串数目 II

思路

同题目 3。

代码

/*
 * @lc app=leetcode.cn id=3298 lang=cpp
 *
 * [3298] 统计重新排列后包含另一个字符串的子字符串数目 II
 */

// @lc code=start
class Solution
{
public:
    long long validSubstringCount(string word1, string word2)
    {
        vector<int> cnt(26, 0);
        for (char &c : word2)
            cnt[c - 'a']--;
        int shortage = 0;
        for (int i = 0; i < 26; i++)
            if (cnt[i] < 0)
                shortage++;

        int len1 = word1.size();
        long long ans = 0LL;
        for (int left = 0, right = 0; left < len1; left++)
        {
            while (right < len1 && shortage > 0)
            {
                cnt[word1[right] - 'a']++;
                if (cnt[word1[right] - 'a'] == 0)
                    shortage--;
                right++;
            }

            if (shortage == 0)
                ans += len1 - right + 1;

            cnt[word1[left] - 'a']--;
            if (cnt[word1[left] - 'a'] == -1)
                shortage++;
        }

        return ans;
    }
};
// @lc code=end

复杂度分析

时间复杂度:O(n+m+∣Σ∣),其中 n 是字符串 word1 的长度,m 是字符串 word2 的长度,∣Σ∣=26 是字符集大小。

空间复杂度:O(∣Σ∣),其中 ∣Σ∣=26 是字符集大小。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UestcXiye

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值