From c3e72eb840f5affbfc2298f91303d338d175a9c4 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Mon, 1 May 2023 22:32:26 +0800 Subject: [PATCH] add: 230501-CN --- cpp/2305/230501-CN.cpp | 32 ++++++++++++++++++++++++++++++++ cpp/2305/CMakeLists.txt | 7 +++++++ cpp/CMakeLists.txt | 3 ++- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 cpp/2305/230501-CN.cpp create mode 100644 cpp/2305/CMakeLists.txt diff --git a/cpp/2305/230501-CN.cpp b/cpp/2305/230501-CN.cpp new file mode 100644 index 0000000..e6b356a --- /dev/null +++ b/cpp/2305/230501-CN.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +/** + * 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&; + static int numOfMinutes(int, int, CVIR, CVIR); +}; + +int Solution::numOfMinutes(int n, int headID, CVIR manager, CVIR informTime) { + std::vector ans(n, -1); + ans[headID] = 0; + std::function 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; +} diff --git a/cpp/2305/CMakeLists.txt b/cpp/2305/CMakeLists.txt new file mode 100644 index 0000000..76ef743 --- /dev/null +++ b/cpp/2305/CMakeLists.txt @@ -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) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index dd064e2..ca2c4d9 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -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)