add: 230501-CN

This commit is contained in:
Eatswap 2023-05-01 22:32:26 +08:00
parent 111c4692c1
commit c3e72eb840
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
3 changed files with 41 additions and 1 deletions

32
cpp/2305/230501-CN.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <algorithm>
#include <forward_list>
#include <functional>
#include <vector>
/**
* 1376. Time Needed to Inform All Employees
*
* A company has n employees with a unique ID for each employee from 0 to n - 1. The head of the company is the one with headID.
* Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also, it is guaranteed that the subordination relationships have a tree structure.
* The head of the company wants to inform all the company employees of an urgent piece of news. He will inform his direct subordinates, and they will inform their subordinates, and so on until all employees know about the urgent news.
* The i-th employee needs informTime[i] minutes to inform all of his direct subordinates (i.e., After informTime[i] minutes, all his direct subordinates can start spreading the news).
* Return the number of minutes needed to inform all the employees about the urgent news.
*/
class Solution {
public:
using CVIR = const std::vector<int>&;
static int numOfMinutes(int, int, CVIR, CVIR);
};
int Solution::numOfMinutes(int n, int headID, CVIR manager, CVIR informTime) {
std::vector<int> ans(n, -1);
ans[headID] = 0;
std::function<int(int)> dp = [&](int cur) {
return ans[cur] >= 0 ? ans[cur] : ans[cur] = dp(manager[cur]) + informTime[manager[cur]];
};
int ret = 0;
for (int i = 0; i < n; ++i)
ret = std::max(ret, dp(i));
return ret;
}

7
cpp/2305/CMakeLists.txt Normal file
View File

@ -0,0 +1,7 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.24)
PROJECT(2305)
SET(CMAKE_CXX_STANDARD 23)
SET(CMAKE_EXPORT_COMPILE_COMMANDS true)
ADD_EXECUTABLE(2305 230501-CN.cpp)

View File

@ -30,5 +30,6 @@ ADD_EXECUTABLE(leetcode-cpp main.cpp 2302/230218.cpp 2302/230218-CN.cpp 2302/230
# ADD_SUBDIRECTORY(2210)
# ADD_SUBDIRECTORY(2302)
# ADD_SUBDIRECTORY(2303)
ADD_SUBDIRECTORY(2304)
# ADD_SUBDIRECTORY(2304)
ADD_SUBDIRECTORY(2305)
ADD_SUBDIRECTORY(more)