【Lintcode】1903. Department Statistics

Subject address:

https://www.lintcode.com/problem/department-statistics/description

Given a list of two strings EEE andFFFEEThe format of the string in E is id + name + department name, separated by a comma space, representing the information of an employee;FFThe number of strings in F is id1 + id2, also separated by comma spaces, which means that the employees corresponding to these two ids are friends (friend relationships are not transitive, only symmetric). Ask the number of employees in each department who have people from other departments as friends. The title guarantees that the id corresponds to the employee one by one.

Open two hash tables, one stores which employee IDs each department has, and the other stores which department each id belongs to. After saving, traverse the array FFF , take out two ids, if the two ids belong to the same department, they will not be counted, otherwise it means that id1 and id2 have friends who do not belong to the same department, then count. It should be noted here that the id must be stored in the hash table when counting, instead of adding one to the count, this will cause repeated calculations. code show as below:

import java.util.*;

public class Solution {
    
    
    /**
     * @param employees:   information of the employees
     * @param friendships: the friendships of employees
     * @return: return the statistics
     */
    public List<String> departmentStatistics(List<String> employees, List<String> friendships) {
    
    
        // write your code here.
        Map<String, Set<Integer>> deptEmps = new HashMap<>();
        Map<Integer, String> idToDept = new HashMap<>();
        for (String employee : employees) {
    
    
            String[] split = employee.split(", ");
            String dept = split[2];
            int id = Integer.parseInt(split[0]);
            // 存一下每个部门有哪些人
            deptEmps.putIfAbsent(dept, new HashSet<>());
            deptEmps.get(dept).add(id);
            
            idToDept.put(id, dept);
        }
        
        Map<String, Set<Integer>> count = new HashMap<>();
        for (String friendship : friendships) {
    
    
            String[] split = friendship.split(", ");
            int id1 = Integer.parseInt(split[0]), id2 = Integer.parseInt(split[1]);
            String dept1 = idToDept.get(id1), dept2 = idToDept.get(id2);
            if (dept1.equals(dept2)) {
    
    
                continue;
            }
            
            // 做计数
            count.putIfAbsent(dept1, new HashSet<>());
            count.get(dept1).add(id1);
            count.putIfAbsent(dept2, new HashSet<>());
            count.get(dept2).add(id2);
        }
        
        List<String> res = new ArrayList<>();
        for (Map.Entry<String, Set<Integer>> entry : deptEmps.entrySet()) {
    
    
            String dept = entry.getKey();
            int total = entry.getValue().size(), num = count.getOrDefault(dept, new HashSet<>()).size();
            res.add(dept + ": " + num + " of " + total);
        }
        
        return res;
    }
}

Time and space complexity O (sl E + l F) O(sl_E+l_F)O(slE+lF) s s s isEEThe longest string length in E.

Guess you like

Origin blog.csdn.net/qq_46105170/article/details/112617677