LeetCode(Weekly Contest 187)题解

0. 前言

1. 题解

1.1 5400. 旅行终点站(1436. Destination City)

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)

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)

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)

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. 参考文献