LeetCode 第390场周赛个人题解

目录

[100245. 每个字符最多出现两次的最长子字符串](#100245. 每个字符最多出现两次的最长子字符串)

原题链接

思路分析

AC代码

[100228. 执行操作使数据元素之和大于等于 K](#100228. 执行操作使数据元素之和大于等于 K)

原题链接

思路分析

AC代码

[100258. 最高频率的 ID](#100258. 最高频率的 ID)

原题链接

思路分析

AC代码

[100268. 最长公共后缀查询](#100268. 最长公共后缀查询)

原题链接

思路分析

AC代码


100245. 每个字符最多出现两次的最长子字符串

原题链接

每个字符最多出现两次的最长子字符串 - 力扣 (LeetCode) 竞赛

思路分析

枚举

直接遍历每一个子串,然后统计那些最大字符频率小于2的就行了

时间复杂度O(n^2)

AC代码

cpp 复制代码
class Solution {
public:
    int maximumLengthSubstring(string s) {
        int n = s.size(), ret = 1;
        for(int len = 1; len <= n; len++){
            for(int i = 0; i <= n - len; i++){
                int cnt[26]{0};
                bool f = 0;
                for(int j = i; j < i + len; j++)
                    f = f || (++cnt[s[j] - 'a'] > 2);
                if(f) continue;
                ret = max(ret, len);break;
            }
        }
        return ret;
    }
};

100228. 执行操作使数据元素之和大于等于 K

原题链接

执行操作使数据元素之和大于等于 K - 力扣 (LeetCode) 竞赛

思路分析

贪心

最优解一定是自增若干次,然后再复制若干次

**证明:**如果存在自增、复制交替出现,或者先复制若干次再自增若干次,我们将自增的操作全都放在前面,复制的操作都放在后面,得到的元素和会变大,因为最终的元素数目一样,而这样最大化了每个元素,故得证

那么我们只需要枚举最终每个元素的值就行了,从1枚举到k

时间复杂度:O(k)

AC代码

cpp 复制代码
class Solution {
public:
    int minOperations(int k) {
        int ret = k;
        for(int i = 1; i <= k; i++)
            ret = min(ret, (i - 1) + (int)ceil(1.0 * k / i) - 1);
        return ret;
    }
};

100258. 最高频率的 ID

原题链接

最高频率的 ID - 力扣 (LeetCode) 竞赛

思路分析

线段树

每次对区间内某个数的频率进行修改,然后求修改后区间内的最大频率

这就完美对应了线段树的单点修改和区间最值,我们只需要写一下线段树的递归建树和单点修改的函数即可,码量巨小

时间复杂度O(mlogn),m为操作次数,n为区间大小

AC代码

cpp 复制代码
struct node{
    int l, r;
    long long s;
}tr[400010];
#define lc p << 1
#define rc p << 1 | 1
void build(int p, int l, int r){
    tr[p] = {l, r, 0};
    if(l == r) return;
    int m = (l + r) >> 1;
    build(lc, l, m), build(rc, m + 1, r);
}
void update(int p, int x, int k){
    if(tr[p].l == x && tr[p].r == x){
        tr[p].s += k; return;
    }
    int m = (tr[p].l + tr[p].r) >> 1;
    if(x > m) update(rc, x, k);
    else update(lc, x, k);
    tr[p].s = max(tr[lc].s, tr[rc].s);
}
class Solution {
public:
    vector<long long> mostFrequentIDs(vector<int>& nums, vector<int>& freq) {
        build(1, 1, *max_element(nums.begin(), nums.end()));
        vector<long long> ret;
        for(int i = 0, n = nums.size(); i < n; i++){
            update(1, nums[i], freq[i]);
            ret.emplace_back(tr[1].s);
        }
        return ret;
    }
};

100268. 最长公共后缀查询

原题链接

最长公共后缀查询 - 力扣 (LeetCode) 竞赛

思路分析

字典树

考虑要能快速的求出查询字符串和字符串集合中的最长公共后缀,我们提前将字符串集合中的字符串倒序插入字典树,然后边插入边统计路径上的最短字符串的下标,这个值是存在路径上节点中的

然后对于每个查询去在字典树上沿着路径往下走就行了,走不动就退出

然后答案就是当前节点存的那个下标

时间复杂度O(Σlen(wordsContaineri) + Σlen(wordsQueryi))

AC代码

cpp 复制代码
struct node{
    unordered_map<char, node*> ch;
    int idx = -1;
}*root, *cur;
class Solution {
public:
    vector<int> stringIndices(vector<string>& wordsContainer, vector<string>& wordsQuery) {
        root = new node;
        vector<int> ret;
        int n = wordsContainer.size(), mi = 0;
        for(int i = 0; i < n; i++){
            cur = root;
            if(wordsContainer[i].size() < wordsContainer[mi].size()) mi = i;
            for(int j = (int)wordsContainer[i].size() - 1; j >= 0; j--){
                char x = wordsContainer[i][j];
                if(!cur->ch.count(x)) cur->ch[x] = new node;
                cur = cur->ch[x];
                if(cur->idx == -1) cur->idx = i;
                else if(wordsContainer[cur->idx].size() > wordsContainer[i].size()) cur->idx = i;
            }
        }
        
        n = wordsQuery.size();
        for(int i = 0; i < n; i++){
            cur = root;
            for(int j = wordsQuery[i].size() - 1; j >= 0; j--){
                char x = wordsQuery[i][j];
                if(cur->ch.count(x)) cur = cur->ch[x];
                else break;
            }
            if(cur != root)
                ret.emplace_back(cur->idx);
            else
                ret.emplace_back(mi);
        }
        return ret;
    }
};
相关推荐
江畔柳前堤2 小时前
大语言模型分布式训练:从并行策略到万卡工程的系统梳理
人工智能·分布式·深度学习·算法·目标检测·机器学习·语言模型
Forever Nore3 小时前
LeetCode 4 寻找两个正序数组的中位数 - 二分
算法·leetcode
罗西的思考4 小时前
【OpenClaw具身硬件】MiniClaw 阅读笔记---(1)基础
人工智能·算法·机器学习
蛋先生DX5 小时前
大模型参数存储格式揭秘:BF不是男朋友
深度学习·算法·llm
爱跳舞的烤冷面6 小时前
自学嵌入式第22天(数据结构——哈希)
数据结构·算法·哈希算法
猎嘤一号6 小时前
博弈论(Game Theory)的理论、算法与工程
人工智能·算法·安全·博弈论
月光船幽幽6 小时前
影子模式下保护 logits 不被修改
人工智能·python·算法
马拉AI6 小时前
腾讯开源 Agent 记忆系统,AI“换对话就忘”的问题有了新解法(附安装使用教程)
人工智能·算法·开源·科研
地平线开发者8 小时前
征程6工具链模型X86推理方式说明
算法
地平线开发者8 小时前
【征程6】校准量化中HistogramObserver解析
算法