[leetcode] 周赛 211

两个相同字符之间的最长子字符串

class Solution {
public:
int maxLengthBetweenEqualCharacters(string s)
{
int maxval = -1;
unordered_map<char, int> m;
int len = s.length();
for (int i=0; i<len; i++)
{
if (m.count(s[i]) == 1)
maxval = max(maxval, i-m[s[i]]-1);
else
m[s[i]] = i;
}
return maxval;
}
};


带阈值的图连通性

class Solution {
public:
vector<int> root;
vector<bool> res;
vector<bool> areConnected(int n, int threshold, vector<vector<int>>& queries)
{
root.resize(n + 1, -1);
for (int i=1; i<=n; i++)
{
for (int j=i+1; j<=n; j++)
{
if (check(n, threshold, j, i))
merge(i,j);
}
}
for (auto &v: queries)
res.push_back(find(v[0]) == find(v[1]));
return res;
}
void merge(int x, int y)
{
x = find(x), y = find(y);
if (x != y) root[y] = x;
}
int find(int x)
{
return root[x] == -1 ? x : (root[x] = find(root[x]));
}
int gcd(int a, int b)  // require a >= b
{
return (b == 0) ? a : gcd(b, a % b);
}
bool check(int n, int threshold, int x, int y)
{
return gcd(x, y) > threshold;
}
};


for (int i=1; i<=n; i++)
for (int j=i+i; j<=n; j+=i)
if (check(n, threshold, j, i))
merge(i, j);


$\sum_{i=1}^{n}\frac{n}{i} = n \sum_{i=1}^{n}\frac{1}{i}$

$\sum_{i=1}^{n}\frac{1}{i} = \int_{1}^{n} \frac{1}{x}dx = \ln{n}$

无矛盾的最佳球队

• 同一年龄
• 分数小于等于 data[i] 的分数

$dp[i] = max(score[i], score[i] + dp[j]), 0 \le j < i$

class Node
{
public:
int score, age;
Node(int s, int a): score(s), age(a){}
bool operator < (const Node &n) const
{
return age < n.age || (age == n.age && score < n.score);
}
};
class Solution {
public:
int bestTeamScore(vector<int>& scores, vector<int>& ages)
{
int n = scores.size();
vector<Node> data;
for (int i=0; i<n; i++)
data.push_back(Node(scores[i], ages[i]));
sort(data.begin(), data.end());
vector<int> dp(n);
int maxval = data[0].score;
for (int i=0; i<n; i++)
{
dp[i] = data[i].score;
for (int j=0; j<i; j++)
{
if (data[j].age == data[i].age || data[j].score <= data[i].score)
dp[i] = max(dp[i], dp[j] + data[i].score);
}
maxval = max(maxval, dp[i]);
}
return maxval;
}
};


posted @ 2020-10-18 17:26  sinkinben  阅读(26)  评论(0编辑  收藏