标签:差值 签到 ISE exactly string nbsp problems 代码 matching
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; } };
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; } };
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; } };
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]; } };
LeetCode(Weekly Contest 187)题解
标签:差值 签到 ISE exactly string nbsp problems 代码 matching
原文地址:https://www.cnblogs.com/wangao1236/p/12821420.html