Leetcode 第 113 场双周赛题解
Leetcode 第 113 场双周赛题解
题目1:2855. 使数组成为递增数组的最少右移次数
思路
问题等价于:
- 数组 nums 有至多两段递增子数组;
- 如果有两段,我们需要把第二段拼在第一段前面,所以还需要满足 nums[0]>nums[n−1],否则无法构成递增数组。
算法:
- 初始化 k = -1。
- 遍历数组 nums,两两比较相邻元素 nums[i] 和 nums[(i+1)%n],如果发现有 nums[(i + 1) % n] < nums[i],记录下标 k = i。
- 继续遍历下标,如果又发现 nums[(i + 1) % n] < nums[i],此时 k != -1,不符合数组 nums 有至多两段递增子数组的要求,返回 -1。
- 否则,右移次数 = n-k-1,返回该值。
代码
/*
* @lc app=leetcode.cn id=2855 lang=cpp
*
* [2855] 使数组成为递增数组的最少右移次数
*/
// @lc code=start
class Solution
{
public:
int minimumRightShifts(vector<int> &nums)
{
int n = nums.size();
int k = -1;
for (int i = 0; i < n; i++)
{
if (nums[(i + 1) % n] < nums[i])
{
if (k == -1)
k = i;
else
return -1;
}
}
return k == -1 ? 0 : n - k - 1;
}
};
// @lc code=end
复杂度分析
时间复杂度:O(n),其中 n 是数组 nums 的元素个数。
空间复杂度:O(1)。
题目2:2856. 删除数对后的最小数组长度
思路
假设数组 nums 中元素 x 出现次数最多,其出现次数为 maxCount。
分类讨论:
- 如果 2 * maxCount > n,其余所有 n − maxCount 个数都要与 x 消除,所以最后剩下 n - 2 * (n-maxCount) = 2 * maxCount - n 个数。
- 如果 2 * maxCount <= n 且 n 是偶数,那么可以把其余数消除至剩下 maxCount 个数,然后再和 x 消除,最后剩下 0 个数。
- 如果 2 * maxCount <= n 且 n 是奇数,同上,最后剩下 1 个数。
代码
/*
* @lc app=leetcode.cn id=2856 lang=cpp
*
* [2856] 删除数对后的最小数组长度
*/
// @lc code=start
// 哈希 + 分类讨论
class Solution
{
public:
int minLengthAfterRemovals(vector<int> &nums)
{
// 特判
if (nums.empty())
return 0;
int n = nums.size();
// 统计数组 nums 中各元素的出现次数
unordered_map<int, int> cnt;
for (int &num : nums)
cnt[num]++;
int maxCount = 0;
for (auto &[_, c] : cnt)
maxCount = max(maxCount, c);
return max(2 * maxCount - n, n % 2);
}
};
// @lc code=end
复杂度分析
时间复杂度:O(n),其中 n 是数组 nums 的元素个数。
空间复杂度:O(n),其中 n 是数组 nums 的元素个数。
题目3:2857. 统计距离为 k 的点对
思路
代码
/*
* @lc app=leetcode.cn id=2857 lang=cpp
*
* [2857] 统计距离为 k 的点对
*/
// @lc code=start
// 哈希
class Solution
{
public:
int countPairs(vector<vector<int>> &coordinates, int k)
{
int n = coordinates.size();
unordered_map<long long, int> cnt;
int count = 0;
for (vector<int> &coordinate : coordinates)
{
int x = coordinate[0], y = coordinate[1];
// 令 x1 ^ x2 = i,i 的范围为 [0, k]
for (int i = 0; i <= k; i++)
{
// 把 (x, y) 压缩成一个整数,例如 1e6 * x + y
auto iter = cnt.find(1000000LL * (x ^ i) + (y ^ (k - i)));
if (iter != cnt.end())
count += iter->second;
}
cnt[1000000LL * x + y]++;
}
return count;
}
// 暴力
// class Solution
// {
// public:
// int countPairs(vector<vector<int>> &coordinates, int k)
// {
// int n = coordinates.size();
// int count = 0;
// for (int i = 0; i < n - 1; i++)
// for (int j = i + 1; j < n; j++)
// {
// int x1 = coordinates[i][0], y1 = coordinates[i][1];
// int x2 = coordinates[j][0], y2 = coordinates[j][1];
// int d = (x1 ^ x2) + (y1 ^ y2);
// if (d == k)
// count++;
// }
// return count;
// }
// };
};
// @lc code=end
复杂度分析
时间复杂度:O(n * k),其中 n 是数组 coordinates 的元素个数。
空间复杂度:O(n),其中 n 是数组 coordinates 的元素个数。
题目4:2858. 可以到达每一个节点的最少边反转次数
思路
换根 DP 模板题。
题解:【模板】换根 DP(Python/Java/C++/Go/JS)
代码
/*
* @lc app=leetcode.cn id=2858 lang=cpp
*
* [2858] 可以到达每一个节点的最少边反转次数
*/
// @lc code=start
class Solution
{
public:
vector<int> minEdgeReversals(int n, vector<vector<int>> &edges)
{
// g[x] 表示 x 的所有邻居
vector<vector<pair<int, int>>> g(n);
for (auto &edge : edges)
{
int x = edge[0], y = edge[1];
// x->y 视为正向,标记为 1;反向标记为 -1
g[x].push_back(pair<int, int>(y, 1));
g[y].push_back(pair<int, int>(x, -1));
}
// size[i] 为 子树 i 的大小
// vector<int> size(n, 1); // 注意这里初始化成 1 了,下面只需要累加儿子的子树大小
vector<int> answer(n);
function<void(int, int)> dfs = [&](int x, int father)
{
// 遍历 x 的邻居 y
for (auto &[y, dir] : g[x])
if (y != father) // 避免访问父节点
{
answer[0] += dir < 0;
dfs(y, x); // x 是 y 的父节点
}
};
dfs(0, -1); // 0 没有父节点
function<void(int, int)> reRoot = [&](int x, int father)
{
// 遍历 x 的邻居 y
for (auto &[y, dir] : g[x])
if (y != father) // 避免访问父节点
{
answer[y] = answer[x] + dir;
reRoot(y, x); // x 是 y 的父节点
}
};
reRoot(0, -1); // 0 没有父节点
return answer;
}
};
// @lc code=end
复杂度分析
时间复杂度:O(n),其中 n 是树的节点个数。DFS 两次,每次 DFS 会递归访问每个节点恰好一次,所以时间复杂度为 O(n)。
空间复杂度:O(n),其中 n 是树的节点个数。