LeetCode(Weekly Contest 187)题解
Posted 西凉风雷
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(Weekly Contest 187)题解相关的知识,希望对你有一定的参考价值。
0. 前言
- 这周五一小长假,祝大家节日愉快
- 中文版地址:https://leetcode-cn.com/contest/weekly-contest-187/
- 英文版地址:https://leetcode.com/contest/weekly-contest-187/
1. 题解
1.1 5400. 旅行终点站(1436. Destination City)
- 中文版题目描述:https://leetcode-cn.com/problems/destination-city/
- 英文版题目描述:https://leetcode.com/problems/destination-city/
- 思路:签到题
- 一个 Set 记录所有城市,一个 Map 记录记录所有城市的出度
- 初度为 0 的就是最终答案
- 代码如下:
class Solution { public: string destCity(vector<vector<string>>& paths) { unordered_map<string, int> cnt; set<string> cities; for (auto v : paths) { cnt[v[0]]++; cities.insert(v[0]); cities.insert(v[1]); } string ans = ""; for (auto v : cities) { if (cnt[v] == 0) ans = v; } return ans; } };
1.2 5401. 是否所有 1 都至少相隔 k 个元素(1437. Check If All 1‘s Are at Least Length K Places Away)
- 中文版题目描述:https://leetcode-cn.com/problems/check-if-all-1s-are-at-least-length-k-places-away/
- 英文版题目描述:https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/
- 思路:签到题
- 一个数组记录所有 1 的位置
- 求相邻两个元素之间差值是否大于等于 k+1
- 代码如下:
class Solution { public: bool kLengthApart(vector<int>& nums, int k) { vector<int> pos; int n = nums.size(); for (int i = 0 ; i < n ; i++) { if (nums[i] == 1) { pos.push_back(i); } } n = pos.size(); bool ans = true; for (int i = 1 ; i < n ; i++) { if (pos[i] - pos[i-1] < k+1) { ans = false; break; } } return ans; } };
1.3 5402. 绝对差不超过限制的最长连续子数组(1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit)
- 中文版题目描述:https://leetcode-cn.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/
- 英文版题目描述:https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/
- 思路:滑动窗口,并通过一个 map 维护滑动窗口内的最大和最小值
- 代码如下:
class Solution { public: int longestSubarray(vector<int>& nums, int limit) { map<int, int> cnt; int left = 0, n = nums.size(), ans = 0; for (int right = 0 ; right < n ; right++) { cnt[nums[right]]++; if (cnt.rbegin()->first - cnt.begin()->first <= limit) { ans = max(right - left + 1, ans); continue; } cnt[nums[left]]--; if (cnt[nums[left]] == 0) { cnt.erase(nums[left]); } left++; } return ans; } };
1.4 5403. 有序矩阵中的第 k 个最小数组和(1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows)
- 中文版题目描述:https://leetcode-cn.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/
- 英文版题目描述:https://leetcode.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/
- 思路:二分法或者直接暴力(k 最大 200)
- 其实本题类似 378. 有序矩阵中第K小的元素,二分法求小于等于当前值的个数,大于等于 k 则更新 right,否则为 left
- 本题求小于等于当前值的个数借助递归,每层递归枚举矩阵的一行
- 暴力思想类似,枚举每一行,每一行中的某一列选一个加入计算
- 重点是控制数量,当前的结果都需要控制最大存前 k 个结果
- 暴力代码更短,但是耗时非常多,卡线过的
- 二分法代码如下:
class Solution { public: void dfs(vector<vector<int>>& mat, int cur, int m, int n, int sum, int mid, int& cnt, int k) { if (cur == m || sum > mid || cnt > k) return; dfs(mat, cur+1, m, n, sum, mid, cnt, k); for (int i = 1 ; i < n ; i++) { if (sum + mat[cur][i] - mat[cur][0] <= mid) { cnt++; dfs(mat, cur+1, m, n, sum + mat[cur][i] - mat[cur][0], mid, cnt, k); } else { break; } } } int kthSmallest(vector<vector<int>>& mat, int k) { int m = mat.size(), n = mat[0].size(); int left = 0, right = 0; for (int i = 0 ; i < m ; i++) { left += mat[i][0]; right += mat[i][n-1]; } int init = left; while (left < right) { int mid = (right - left) / 2 + left; int cnt = 1; dfs(mat, 0, m, n, init, mid, cnt, k); if (cnt >= k) { right = mid; } else { left = mid + 1; } } return left; } };
- 暴力代码如下:
class Solution { public: int kthSmallest(vector<vector<int>>& mat, int k) { int m = mat.size(), n = mat[0].size(); vector<int> res(mat[0]); for (int i = 1 ; i < m ; i++) { multiset<int> tmp; for (auto cur : res) { for (auto next : mat[i]) { tmp.insert(cur + next); } } res.assign(tmp.begin(), tmp.end()); res.resize(min(k, (int)res.size())); } return res[k-1]; } };
3. 参考文献
- https://leetcode-cn.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/solution/longest-continuous-subarray-by-ikaruga/
- https://leetcode-cn.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/solution/er-fen-by-newbie-19-3/
以上是关于LeetCode(Weekly Contest 187)题解的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode(Weekly Contest 183)题解