leetcode第354场周赛补题

6889. 特殊元素平方和 - 力扣(LeetCode)

思路:模拟

class Solution {
public:
    int sumOfSquares(vector<int>& nums) {
        int res = 0;
        for(int i = 0; i < nums.size(); i ++ )
        {
            if(nums.size() % (i + 1) == 0) res += nums[i] * nums[i];
        }
        return res;
    }
};

6929. 数组的最大美丽值 - 力扣(LeetCode)

思路:排序+双指针

class Solution {
public:
    int maximumBeauty(vector<int>& nums, int k) {
        int n = nums.size();
        int res = 0, left = 0;
        sort(nums.begin(), nums.end());
        for(int right = 0; right < n; right ++ )
        {
            while (nums[right] - nums[left] > k * 2) left ++ ;
            res = max(res, right - left + 1);
        }
        return res;
    }
};

6927. 合法分割的最小下标 - 力扣(LeetCode)

思路:哈希+枚举

class Solution {
public:
    int minimumIndex(vector<int>& nums) {
        map<int, int> mp;
        int k = 0, cnt = 0;
        for(int t : nums)
        {
            mp[t] ++ ;
            if(mp[t] > cnt)
            {
                cnt = mp[t];
                k = t;
            }
        }
        int n = nums.size();
        int count = 0;
        for(int i = 0; i < n; i ++ )
        {
            if(nums[i] == k) count ++ ;
            if(count > (i + 1) / 2 && (cnt - count) > (n - 1 - i) / 2) return i;
        }
        
        return -1;
    }
};

6924. 最长合法子字符串的长度 - 力扣(LeetCode)

思路:哈希+双指针

class Solution {
public:
    int longestValidSubstring(string word, vector<string>& forbidden) {
        unordered_set<string> se{forbidden.begin(), forbidden.end()};
        int res = 0, left = 0, n = word.size();
        for(int right = 0; right < n; right ++ )
        {
            for(int i = right; i >= left && i > right - 10; i -- )
            {
                if(se.count(word.substr(i, right - i + 1)))
                {
                    left = i + 1;
                    break;
                }
            }
            res = max(res, right - left + 1);
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值