LeetCode 第390场周赛个人题解

目录

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

原题链接

思路分析

AC代码

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

原题链接

思路分析

AC代码

100258. 最高频率的 ID

原题链接

思路分析

AC代码

100268. 最长公共后缀查询

原题链接

思路分析

AC代码


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

原题链接

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

思路分析

枚举

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

时间复杂度O(n^2)

AC代码

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代码

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代码

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代码

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

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

EQUINOX1

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

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

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

打赏作者

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

抵扣说明:

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

余额充值