#define fo(i, a, b) for(int i = a; i < b; ++i)
class Solution {
public:
int twoSumLessThanK(vector<int>& A, int K) {
sort(A.begin(), A.end());
int len = A.size();
int maxsum = -1;
fo(i, 0, len)
{
int cursum = -1;
for(int j = len - 1; j > i; --j)
{
if(A[j] + A[i] < K)
{
cursum = A[j] + A[i];
break;
}
}
maxsum = max(maxsum, cursum);
}
return maxsum;
}
};
Find K-Length Substrings With No Repeated Characters
int father[101];
class Solution {
public:
int find(int x)
{
int t = x;
while (father[t] != t)
t = father[t];
return t;
}
void mix(int a, int b)
{
int fa = find(a), fb = find(b);
if (fa != fb)
father[fa] = fb;
}
int earliestAcq(vector<vector<int>>& logs, int N) {
//按时间排序
sort(logs.begin(), logs.end(),
[&](const vector<int>& a, const vector<int>& b)
{
return a[0] < b[0];
});
int res = INT_MAX;
vector<int> people(N, 0);
for (int i = 0; i < N; ++i)
father[i] = i;
for (auto v : logs)
{
if (find(v[1]) != find(v[2]))
{
N--;
mix(v[1], v[2]);
}
if (N == 1) res = min(res, v[0]);
}
if (res == INT_MAX) return -1;
else return res;
}
};