第 374 场 LeetCode 周赛题解

A 找出峰值

第 374 场 LeetCode 周赛题解_第1张图片

枚举

class Solution {
public:
    vector<int> findPeaks(vector<int> &mountain) {
        int n = mountain.size();
        vector<int> res;
        for (int i = 1; i < n - 1; i++)
            if (mountain[i] > mountain[i - 1] && mountain[i] > mountain[i + 1])
                res.push_back(i);
        return res;
    }
};

B 需要添加的硬币的最小数量

第 374 场 LeetCode 周赛题解_第2张图片

贪心:对 c o i n s coins coins 升序排序,枚举 c o i n s [ i ] coins[i] coins[i] ,设当前已经可获得 [ 0 , r e a c h ] [0,reach] [0,reach] 范围内的任意整数金额,若 c o i n s [ i ] ≤ r e a c h + 1 coins[i] \le reach + 1 coins[i]reach+1 ,则由 c o i n s [ 0 , i ] coins[0,i] coins[0,i] 的子序列可得到 [ 0 , r e a c h + c o i n s [ i ] ] [0,reach+coins[i]] [0,reach+coins[i]] 范围内的任意整数金额度,否则需要添加一个尽可能大的硬币 r e a c h + 1 reach + 1 reach+1

class Solution {
public:
    int minimumAddedCoins(vector<int> &coins, int target) {
        sort(coins.begin(), coins.end());
        int res = 0;
        int e, i, reach;
        for (e = 1, i = 0, reach = 0; reach < target && i < coins.size();) {
            if (coins[i] <= reach + 1) {
                reach += coins[i++];
            } else {
                res++;
                reach += reach + 1;
            }
        }
        for (; reach < target;) {
            res++;
            reach += reach + 1;
        }
        return res;
    }
};

C 统计完全子字符串

第 374 场 LeetCode 周赛题解_第3张图片

枚举 + 前缀和:首先用条件2将 w o r d word word 划分成若干满足子字符串,使得各子字符串内部任意相邻字符满足条件2,然后只需在各个子字符串中找满足条件1的子字符串,这样的字符串的长度只能是 k , 2 k , ⋯   , 26 k k,2k,\cdots,26k k,2k,,26k 其中之一,枚举子字符串可能的长度 l e n len len,然后滑动窗口枚举场长为 l e n len len 的子字符串,用前缀和判断在该子串中的字符数量是否为 k k k

class Solution {
public:
    int cmp(const string &s, int k) {
        int n = s.size();
        int pre[n + 1][26];//前缀和
        for (int j = 0; j < 26; j++)
            pre[0][j] = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < 26; j++)
                pre[i + 1][j] = pre[i][j] + (s[i] - 'a' == j ? 1 : 0);
        int res = 0;
        for (int len = k; len <= s.size() && len <= 26 * k; len += k) {
            for (int l = 0, r = len - 1; r < n; l++, r++) {//子串s[l,r]
                int can = 1;
                for (int i = 0; i < 26; i++) {
                    if (pre[r + 1][i] - pre[l][i] != 0 && pre[r + 1][i] - pre[l][i] != k) {
                        can = 0;
                        break;
                    }
                }
                if (can)
                    res++;
            }
        }
        return res;
    }

    int countCompleteSubstrings(string word, int k) {
        int res = 0;
        for (int i = 0, j = 0, n = word.size(); i < n;) {
            while (j + 1 < n && abs(word[j + 1] - word[j]) <= 2)
                j++;
            res += cmp(word.substr(i, j - i + 1), k);
            i = ++j;
        }
        return res;
    }
};

D 统计感冒序列的数目

第 374 场 LeetCode 周赛题解_第4张图片
第 374 场 LeetCode 周赛题解_第5张图片

计数 + 逆元:设以感冒的人为间隔,未感冒的人可以划分成若干人数为 a i a_i ai 的组: a 1 , ⋯   , a k a_1,\cdots,a_k a1,,ak,总序列数为: ( ∑ a i ) ! ∏ a i ! × ∏ f ( a i ) \frac {(\sum a_i)!} {\prod a_i!} \times \prod f(a_i) ai!(ai)!×f(ai) ,其中当 a i a_i ai 不是与边界相邻的组时 f ( a i ) = 2 a i − 1 f(a_i)=2^{a_i-1} f(ai)=2ai1 ,否则 f ( a i ) = 1 f(a_i)=1 f(ai)=1

class Solution {
public:
    using ll = long long;
    ll mod = 1e9 + 7;

    ll fpow(ll x, ll n) {//快速幂 x^n
        ll res = 1;
        for (ll e = x; n; e = e * e % mod, n >>= 1)
            if (n & 1)
                res = res * e % mod;
        return res;
    }

    ll inv(ll x) {//x 在 mod 下的匿元 
        return fpow(x, mod - 2);
    }

    int numberOfSequence(int n, vector<int> &sick) {
        vector<ll> fact(n + 1, 1), f2(n + 1, 1);
        for (int i = 1; i <= n; i++) {
            fact[i] = fact[i - 1] * i % mod;
            f2[i] = f2[i - 1] * 2 % mod;
        }
        int res = 1;
        int m = sick.size();
        int s = 0;
        if (sick[0] != 0) {//与左边界相邻的组
            s += sick[0];
            res = res * inv(fact[sick[0]]) % mod;
        }
        if (sick[m - 1] != n - 1) {//与右边界相邻的组
            s += n - 1 - sick[m - 1];
            res = res * inv(fact[n - 1 - sick[m - 1]]) % mod;
        }
        for (int i = 0; i < m - 1; i++)//枚举中间的各个分组
            if (sick[i + 1] != sick[i] + 1) {
                int t = sick[i + 1] - sick[i] - 1;
                s += t;
                res = res * inv(fact[t]) % mod;
                res = res * f2[t - 1] % mod;
            }
        res = res * fact[s] % mod;
        return (res + mod) % mod;
    }
};

你可能感兴趣的:(LeetCode,leetcode,算法,贪心,枚举,前缀和,滑动窗口,数学)