日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

天池 在线编程 部门统计(哈希)

發(fā)布時間:2024/7/5 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 天池 在线编程 部门统计(哈希) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • 1. 題目
    • 2. 解題

1. 題目

描述
公司給你提供了所有員工的信息,包括其ID,姓名和所屬部門。
以及他們之間的朋友關(guān)系,每個關(guān)系中由2個ID組成,如 “1, 2” 代表1號員工和2號員工是朋友。
朋友關(guān)系不具有傳遞性,即B、C都是A的朋友,但B和C不一定是朋友。
請計算每個部門中與其它部門的員工有朋友關(guān)系的員工個數(shù)。

所有的輸入中逗號后都跟有一個空格,而且你的程序輸出也要和樣例格式相同。返回的列表對順序沒有要求。員工信息數(shù)量 N <= 50 條。朋友關(guān)系的數(shù)量 M <= 1000 條。員工ID都是100以內(nèi)的數(shù)字。部門數(shù) K <= 20。 示例 輸入: employees = ["1, Bill, Engineer","2, Joe, HR","3, Sally, Engineer","4, Richard, Business","6, Tom, Engineer" ]friendships = ["1, 2","1, 3","3, 4" ]輸出: "Engineer: 2 of 3" "HR: 1 of 1" "Business: 1 of 1"說明: 樣例中,`Engineer`的`1`號員工和`HR`的`2`號員工是朋友關(guān)系, `Engineer` 的`3`號員工和`Business`的`4`號員工是朋友關(guān)系, 所以`Engineer`有`2`個人和其它部門有朋友關(guān)系,輸出"Engineer: 2 of 3“。 此外,HR部門有1人和其他部門有朋友關(guān)系, Business部門有1人和其他部門有朋友關(guān)系。

https://tianchi.aliyun.com/oj/376506598349105305/389682099890885302

2. 解題

class Solution { public:/*** @param employees: information of the employees* @param friendships: the friendships of employees* @return: return the statistics*/vector<string> departmentStatistics(vector<string> &employees, vector<string> &friendships) {// write your code here.unordered_map<string, int> count;unordered_map<string, string> id_dep;for(auto& e : employees){vector<string> t = split(e);id_dep[t[0]] = t[2];count[t[2]]++;}unordered_map<string, unordered_set<string>> p;for(auto& f : friendships){vector<string> t = split(f);if(id_dep[t[0]] != id_dep[t[1]]) // 同一個部門的話,不能計算{ p[id_dep[t[0]]].insert(t[0]);p[id_dep[t[1]]].insert(t[1]);}}vector<string> ans;for(auto& ct : count){int all = ct.second;string dep = ct.first;int num = p[dep].size();ans.push_back(dep + ": " + to_string(num) + " of " + to_string(all));}return ans;}vector<string> split(string& str){vector<string> t;string s;for(auto c : str){if(c ==','){t.push_back(s);s = "";}else if(c != ' ')s.push_back(c);}t.push_back(s);return t;} };

我的CSDN博客地址 https://michael.blog.csdn.net/

長按或掃碼關(guān)注我的公眾號(Michael阿明),一起加油、一起學(xué)習進步!

總結(jié)

以上是生活随笔為你收集整理的天池 在线编程 部门统计(哈希)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。