# leetcode weekly-contest-354

## 特殊元素平方和

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

## 数组的最大美丽值

  class Solution {
public:
int f[100010];

int maximumBeauty(vector &nums, int k) {
for (auto &x: nums) {
f[max(0, x - k)]++;
f[min(100000, x + k) + 1]--;
}
int cnt = 0, ans = 0;
for (int i = 0; i <= 100000; i++) {
cnt += f[i];
ans = max(ans, cnt);
}
return ans;
}
};

## 合法分割的最小下标

class Solution {
public:
int minimumIndex(vector<int> &nums) {
map<int, int> all, cnt;
for (auto &x: nums) all[x]++;
int n = nums.size();
for (int i = 0; i < n; i++) {
all[nums[i]]--;
cnt[nums[i]]++;
if (cnt[nums[i]] * 2 > i + 1)
if (all[nums[i]] * 2 > (n - (i + 1))) return i;
}
return -1;
}
};

## 最长合法子字符串的长度

class Solution {
public:
int longestValidSubstring(string word, vector<string>& forbidden) {
set<string> h(forbidden.begin(), forbidden.end());
int n = word.size(), ans = 0, l = 0;
for (int r = 0; r < n; r++) {
for (int i = r; i >= l; i--) {
if (r - i >= 10) break;
if (h.count(word.substr(i, r - i + 1))) {
l = i + 1;
break;
}
}
ans = max(ans, r - l + 1);
}
return ans;
}
};