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(wordsContainer[i]) + Σlen(wordsQuery[i]))

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;
    }
};
相关推荐
自由的dream1 小时前
0-1背包问题
算法
2401_857297911 小时前
招联金融2025校招内推
java·前端·算法·金融·求职招聘
爱上语文2 小时前
Java LeetCode每日一题
java·开发语言·leetcode
良月澪二3 小时前
CSP-S 2021 T1廊桥分配
算法·图论
wangyue44 小时前
c# 线性回归和多项式拟合
算法
&梧桐树夏4 小时前
【算法系列-链表】删除链表的倒数第N个结点
数据结构·算法·链表
QuantumStack4 小时前
【C++ 真题】B2037 奇偶数判断
数据结构·c++·算法
今天好像不上班4 小时前
软件验证与确认实验二-单元测试
测试工具·算法
wclass-zhengge5 小时前
数据结构篇(绪论)
java·数据结构·算法
何事驚慌5 小时前
2024/10/5 数据结构打卡
java·数据结构·算法