From 390504f4fd8f63c5ff3aa4de1ab924dc3783a9a8 Mon Sep 17 00:00:00 2001 From: lallunallala87 <95435947+lallunallala87@users.noreply.github.com> Date: Fri, 14 Oct 2022 11:26:48 +0530 Subject: [PATCH 1/2] Create number _of_employees_under_manager.java java program to find number of Employees Under every Manager --- number _of_employees_under_manager.java | 99 +++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 number _of_employees_under_manager.java diff --git a/number _of_employees_under_manager.java b/number _of_employees_under_manager.java new file mode 100644 index 0000000..6105333 --- /dev/null +++ b/number _of_employees_under_manager.java @@ -0,0 +1,99 @@ +// Java program to find number of persons under every employee +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class NumberEmployeeUnderManager +{ + // A hashmap to store result. It stores count of employees + // under every employee, the count may by 0 also + static Map result = + new HashMap(); + + // Driver function + public static void main(String[] args) + { + Map dataSet = new HashMap(); + dataSet.put("A", "C"); + dataSet.put("B", "C"); + dataSet.put("C", "F"); + dataSet.put("D", "E"); + dataSet.put("E", "F"); + dataSet.put("F", "F"); + + populateResult(dataSet); + System.out.println("result = " + result); + } + + // This function populates 'result' for given input 'dataset' + private static void populateResult(Map dataSet) + { + // To store reverse of original map, each key will have 0 + // to multiple values + Map> mngrEmpMap = + new HashMap>(); + + // To fill mngrEmpMap, iterate through the given map + for (Map.Entry entry: dataSet.entrySet()) + { + String emp = entry.getKey(); + String mngr = entry.getValue(); + if (!emp.equals(mngr)) // excluding emp-emp entry + { + // Get the previous list of direct reports under + // current 'mgr' and add the current 'emp' to the list + List directReportList = mngrEmpMap.get(mngr); + + // If 'emp' is the first employee under 'mgr' + if (directReportList == null) + { + directReportList = new ArrayList(); + // add a new entry for the mngr with empty directReportList + mngrEmpMap.put(mngr, directReportList); + } + directReportList.add(emp); + } + } + + // Now use manager-Emp map built above to populate result + // with use of populateResultUtil() + + // note- we are iterating over original emp-manager map and + // will use mngr-emp map in helper to get the count + for (String mngr: dataSet.keySet()) + populateResultUtil(mngr, mngrEmpMap); + } + + // This is a recursive function to fill count for 'mgr' using + // mngrEmpMap. This function uses memoization to avoid re- + // computations of subproblems. + private static int populateResultUtil(String mngr, + Map> mngrEmpMap) + { + int count = 0; + + // means employee is not a manager of any other employee + if (!mngrEmpMap.containsKey(mngr)) + { + result.put(mngr, 0); + return 0; + } + + // this employee count has already been done by this + // method, so avoid re-computation + else if (result.containsKey(mngr)) + count = result.get(mngr); + + else + { + List directReportEmpList = mngrEmpMap.get(mngr); + count = directReportEmpList.size(); + for (String directReportEmp: directReportEmpList) + count += populateResultUtil(directReportEmp, mngrEmpMap); + + result.put(mngr, count); + } + return count; + } +} From ed708c1b5f423f3ad00aa07dcd8913bb50898404 Mon Sep 17 00:00:00 2001 From: lallunallala87 <95435947+lallunallala87@users.noreply.github.com> Date: Fri, 14 Oct 2022 11:40:19 +0530 Subject: [PATCH 2/2] Create employee_details.java --- employee_details.java | 98 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 employee_details.java diff --git a/employee_details.java b/employee_details.java new file mode 100644 index 0000000..86584de --- /dev/null +++ b/employee_details.java @@ -0,0 +1,98 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class NumberEmployeeUnderManager +{ + // A hashmap to store result. It stores count of employees + // under every employee, the count may by 0 also + static Map result = + new HashMap(); + + // Driver function + public static void main(String[] args) + { + Map dataSet = new HashMap(); + dataSet.put("A", "C"); + dataSet.put("B", "C"); + dataSet.put("C", "F"); + dataSet.put("D", "E"); + dataSet.put("E", "F"); + dataSet.put("F", "F"); + + populateResult(dataSet); + System.out.println("result = " + result); + } + + // This function populates 'result' for given input 'dataset' + private static void populateResult(Map dataSet) + { + // To store reverse of original map, each key will have 0 + // to multiple values + Map> mngrEmpMap = + new HashMap>(); + + // To fill mngrEmpMap, iterate through the given map + for (Map.Entry entry: dataSet.entrySet()) + { + String emp = entry.getKey(); + String mngr = entry.getValue(); + if (!emp.equals(mngr)) // excluding emp-emp entry + { + // Get the previous list of direct reports under + // current 'mgr' and add the current 'emp' to the list + List directReportList = mngrEmpMap.get(mngr); + + // If 'emp' is the first employee under 'mgr' + if (directReportList == null) + { + directReportList = new ArrayList(); + // add a new entry for the mngr with empty directReportList + mngrEmpMap.put(mngr, directReportList); + } + directReportList.add(emp); + } + } + + // Now use manager-Emp map built above to populate result + // with use of populateResultUtil() + + // note- we are iterating over original emp-manager map and + // will use mngr-emp map in helper to get the count + for (String mngr: dataSet.keySet()) + populateResultUtil(mngr, mngrEmpMap); + } + + // This is a recursive function to fill count for 'mgr' using + // mngrEmpMap. This function uses memoization to avoid re- + // computations of subproblems. + private static int populateResultUtil(String mngr, + Map> mngrEmpMap) + { + int count = 0; + + // means employee is not a manager of any other employee + if (!mngrEmpMap.containsKey(mngr)) + { + result.put(mngr, 0); + return 0; + } + + // this employee count has already been done by this + // method, so avoid re-computation + else if (result.containsKey(mngr)) + count = result.get(mngr); + + else + { + List directReportEmpList = mngrEmpMap.get(mngr); + count = directReportEmpList.size(); + for (String directReportEmp: directReportEmpList) + count += populateResultUtil(directReportEmp, mngrEmpMap); + + result.put(mngr, count); + } + return count; + } +}