日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

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

文章目錄

    • 1. 題目
    • 2. 解題

1. 題目

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

所有的輸入中逗號后都跟有一個空格,而且你的程序輸出也要和樣例格式相同。返回的列表對順序沒有要求。員工信息數量 N <= 50 條。朋友關系的數量 M <= 1000 條。員工ID都是100以內的數字。部門數 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`號員工是朋友關系, `Engineer` 的`3`號員工和`Business`的`4`號員工是朋友關系, 所以`Engineer`有`2`個人和其它部門有朋友關系,輸出"Engineer: 2 of 3“。 此外,HR部門有1人和其他部門有朋友關系, Business部門有1人和其他部門有朋友關系。

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阿明),一起加油、一起學習進步!

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。