第 117 场双周赛
给小朋友们分糖果 I
同T2
给小朋友们分糖果 II
数学
class Solution {
public:long long distributeCandies(int n, int limit) {long long ans = 0;for (int i = 0; i <= min(n, limit); i++) {if (n - i <= limit) {ans += n - i + 1;} else if (n - i <= limit * 2) {ans += limit - (n - i - limit) + 1;}}return ans;}
};
重新排列后包含指定子字符串的字符串数目
DP
class Solution {
public:int stringCount(int n) {const int MOD = 1e9 + 7;auto add = [&](long long &a, long long b) {a = (a + b) % MOD;};long long f[n + 1][2][3][2];memset(f, 0, sizeof(f));f[0][0][0][0] = 1;for (int i = 0; i < n; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 3; k++) for (int l = 0; l < 2; l++) {// 下一个字符填写除了 l, e, t 以外的 23 个字母add(f[i + 1][j][k][l], f[i][j][k][l] * 23);// 下一个字符填写 ladd(f[i + 1][min(j + 1, 1)][k][l], f[i][j][k][l]);// 下一个字符填写 eadd(f[i + 1][j][min(k + 1, 2)][l], f[i][j][k][l]);// 下一个字符填写 tadd(f[i + 1][j][k][min(l + 1, 1)], f[i][j][k][l]);}return f[n][1][2][1];}
};
购买物品的最大开销
最小堆模拟
class Solution {
public:long long maxSpending(vector<vector<int>> &values) {priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;int m = values.size(), n = values[0].size();for (int i = 0; i < m; i++) {pq.emplace(values[i].back(), i);}long long ans = 0;for (int d = 1; d <= m * n; d++) {auto [v, i] = pq.top();pq.pop();ans += (long long) v * d;values[i].pop_back();if (!values[i].empty()) {pq.push({values[i].back(), i});}}return ans;}
};
–