求出每个点到别的点的最短距离,然后在给定范围内遍历。
class Solution {
public:
int findTheCity(int n, vector<vector<int>> &edges, int distanceThreshold) {
pair<int, int> ans(INT_MAX / 2, -1);
vector<vector<int>> dis(n, vector<int>(n, INT_MAX / 2));
vector<vector<int>> vis(n, vector<int>(n, false));
vector<vector<int>> mp(n, vector<int>(n, INT_MAX / 2));
for (auto &eg: edges) {
int from = eg[0], to = eg[1], weight = eg[2];
mp[from][to] = mp[to][from] = weight;
}
for (int i = 0; i < n; ++i) {
dis[i][i] = 0;
for (int j = 0; j < n; ++j) {
int t = -1;
for (int k = 0; k < n; ++k) {
if (!vis[i][k] && (t == -1 || dis[i][k] < dis[i][t])) {
t = k;
}
}
for (int k = 0; k < n; ++k) {
dis[i][k] = min(dis[i][k], dis[i][t] + mp[t][k]);
}
vis[i][t] = true;
}
}
for (int i = 0; i < n; ++i) {
int cnt = 0;
for (int j = 0; j < n; ++j) {
if (dis[i][j] <= distanceThreshold) {
cnt++;
}
}
if (cnt <= ans.first) {
ans = {cnt, i};
}
}
return ans.second;
}
};
因为一天的难度指的是当天最大值,所以要考虑的是某一段内的最大值,由此可以想到单调栈解题。套用模板即可。
class Solution {
public:
int minDifficulty(vector<int>& jobDifficulty, int d) {
int n = jobDifficulty.size();
if (n < d) {
return -1;
}
vector<int> dp(n);
for (int j = 0, ma = 0; j < n; ++j) {
ma = max(ma, jobDifficulty[j]);
dp[j] = ma;
}
for (int i = 1; i < d; ++i) {
stack<pair<int, int>> st;
vector<int> ndp(n);
for (int j = i; j < n; ++j) {
int mi = dp[j - 1];
while (!st.empty() && jobDifficulty[st.top().first] < jobDifficulty[j]) {
mi = min(mi, st.top().second);
st.pop();
}
if (st.empty()) {
ndp[j] = mi + jobDifficulty[j];
} else {
ndp[j] = min(ndp[st.top().first], mi + jobDifficulty[j]);
}
st.emplace(j, mi);
}
swap(dp, ndp);
}
return dp[n - 1];
}
};
class Solution {
public:
vector<int> kWeakestRows(vector<vector<int>>& mat, int k)
{
int cnt = 0;
vector<int> ret;
vector<pair<int, int>> cntPair;
for (int i = 0; i < mat.size(); ++i)
{
cnt = 0;
for (auto n : mat[i])
{
if (n)
cnt++;
else
break;
}
cntPair.push_back(make_pair(i, cnt));
}
sort(
cntPair.begin(), cntPair.end(),
[=](pair<int, int>& a, pair<int, int>& b)
{
return a.second < b.second || (a.second == b.second && a.first < b.first);
}
);
cnt = 0;
for (auto p : cntPair)
{
ret.push_back(p.first);
cnt++;
if (cnt >= k)
break;
}
return ret;
}
};
返回 至少 能删除数组中的一半整数的整数集合的最小大小。
贪心算法:按出现次数排序,从大到小开始加,加到超过一半一定是最少的
class Solution {
public:
int minSetSize(vector<int>& arr) {
unordered_map<int, int> freq;
for (int num: arr) {
++freq[num];
}
vector<int> occ;
for (auto& [k, v]: freq) {
occ.push_back(v);
}
sort(occ.begin(), occ.end(), greater<int>());
int cnt = 0, ans = 0;
for (int c: occ) {
cnt += c;
ans += 1;
if (cnt * 2 >= arr.size()) {
break;
}
}
return ans;
}
};
递归查找一个最接近总和一半的数。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
int sum = 0;
int best = 0;
public:
void dfs(TreeNode* node) {
if (!node) {
return;
}
sum += node->val;
dfs(node->left);
dfs(node->right);
}
int dfs2(TreeNode* node) {
if (!node) {
return 0;
}
int cur = dfs2(node->left) + dfs2(node->right) + node->val;
if (abs(cur*2 - sum) < abs(best*2 - sum)) {
best = cur;
}
return cur;
}
int maxProduct(TreeNode* root) {
dfs(root);
dfs2(root);
return (long long)best * (sum - best) % 1000000007;
}
};
先按照高度和索引记一个数组,然后dp从低点开始遍历一遍即可完成。
class Solution {
public:
int maxJumps(vector<int>& arr, int d) {
int n = arr.size();
vector<int> dp(n, 1);
vector<pair<int, int>> arr2;
for (int i = 0; i < n; ++i) {
arr2.push_back({arr[i], i});
}
sort(arr2.begin(), arr2.end());
for (int i = 0; i < n; ++i) {
int j = arr2[i].second;
for (int k = j - 1; k >= max(0, j - d) && arr[k] < arr[j]; --k) {
dp[j] = max(dp[j], dp[k] + 1);
}
for (int k = j + 1; k <= min(n - 1, j + d) && arr[k] < arr[j]; ++k) {
dp[j] = max(dp[j], dp[k] + 1);
}
}
return *max_element(dp.begin(), dp.end());
}
};
# Write your MySQL query statement below
(select u.name results
from MovieRating mr
left join Users u using(user_id)
group by u.user_id
order by count(movie_id) desc, u.name asc
limit 1)
union all
(select m.title results
from MovieRating mr
left join Movies m using(movie_id)
where date_format(mr.created_at,'%Y-%m') = '2020-02'
group by m.movie_id
order by avg(rating) desc, m.title asc
limit 1)