天池 在线编程 部门统计(哈希)
生活随笔
收集整理的這篇文章主要介紹了
天池 在线编程 部门统计(哈希)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 題目
- 2. 解題
1. 題目
描述
公司給你提供了所有員工的信息,包括其ID,姓名和所屬部門。
以及他們之間的朋友關系,每個關系中由2個ID組成,如 “1, 2” 代表1號員工和2號員工是朋友。
朋友關系不具有傳遞性,即B、C都是A的朋友,但B和C不一定是朋友。
請計算每個部門中與其它部門的員工有朋友關系的員工個數。
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/
長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!
總結
以上是生活随笔為你收集整理的天池 在线编程 部门统计(哈希)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 1796. 字符串中第
- 下一篇: python 接口 、继承、重载运算符