有趣的题目 力扣1057 校园自行车配对

https://leetcode-cn.com/problems/campus-bikes/

class Solution {
public:
    vector<int> assignBikes(vector<vector<int>>& workers, vector<vector<int>>& bikes) {
        map<int, vector<pair<int, int> > > distance;
        //每一个曼哈顿距离的工人-自行车对

        int w[1005] = {0};
        int b[1005] = {0};
        vector<int> ans(workers.size());

        /* 两重循环遍历所有可能,因为循环索引是从小到大的,
            自然符合优先选择索引小的 
        */
        for (int i = 0; i < workers.size(); ++i) {
            for (int j = 0; j < bikes.size(); ++j) {
                int dis = abs(workers[i][0] - bikes[j][0]) + abs(workers[i][1] - bikes[j][1]);
                distance[dis].push_back({i, j});
            }
        }

        for (auto &m : distance) {
            for (auto &p : m.second) {
                if (w[p.first] || b[p.second])
                    continue;
                ans[p.first] = p.second;
                w[p.first] = b[p.second] = 1;
            }
        }

        return ans;
    }
};
全部评论

相关推荐

野猪不是猪🐗:😇:恭喜你以出色的表现成为xxx的一员 😨:您以进入本公司人才库 实际点开:您愿望单中的xxx正在特卖!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务