leetcode第359场周赛补题

7004. 判别首字母缩略词 - 力扣(LeetCode)

思路:枚举

class Solution {
public:
    bool isAcronym(vector& words, string s) {
        string t;
        for(auto str : words)
        {
            t += str[0];
        }
        return t == s;
    }
};

6450. k-avoiding 数组的最小总和 - 力扣(LeetCode)

思路:数学

class Solution {
public:
    int minimumSum(int n, int k) {
        int m = min(n, k / 2);
        return (m * (m + 1) + (2 * k + n - m - 1) * (n - m)) / 2;
    }
};

7006. 销售利润最大化 - 力扣(LeetCode)

思路:线性dp

class Solution {
public:
    int maximizeTheProfit(int n, vector>& offers) {
        vector>> groups(n);
        for(auto offer : offers)
            groups[offer[1]].emplace_back(offer[0], offer[2]);
        vector f(n + 1);
        for(int end = 0; end < n; end ++ )
        {
            f[end + 1] = f[end];
            for(auto &[start, gold] : groups[end])
                f[end + 1] = max(f[end + 1], f[start] + gold);
        }
        return f[n];
    }
};

2831. 找出最长等值子数组 - 力扣(LeetCode)

思路:同向双指针+哈希

class Solution {
public:
    int longestEqualSubarray(vector& nums, int k) {
        int n = nums.size(), res = 0;
        vector> count(n + 1);
        for(int i = 0; i < n; i ++ )
            count[nums[i]].push_back(i - count[nums[i]].size());
        for(auto s : count)
        {
            if(s.size() <= res) continue;
            int left = 0;
            for(int right = 0; right < s.size(); right ++ )
            {
                while (s[right] - s[left] > k) left ++ ;
                res = max(res, right - left + 1);
            }
        }
        return res;
    }
};

你可能感兴趣的:(c++,数据结构,leetcode,算法)