diff --git a/cpp/2206/220623.cpp b/cpp/2206/220623.cpp new file mode 100644 index 0000000..acce4db --- /dev/null +++ b/cpp/2206/220623.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +/** + * 630. Course Schedule III + * There are n different online courses numbered from 1 to n. You are given an array courses where courses[i] = [duration[i], lastDay[i]] indicate that the ith course should be taken continuously for durationi days and must be finished before or on lastDayi. + * You will start on the 1st day and you cannot take two or more courses simultaneously. + * Return the maximum number of courses that you can take. + */ + +class Solution { +public: + static int scheduleCourse(std::vector>& c) { + std::sort(c.begin(), c.end(), [](const auto& x, const auto& y) { + return x[1] < y[1]; + }); + std::priority_queue q; + int t = 0; + for (const auto& x : c) { + if (t + x[0] <= x[1]) { + q.push(x[0]); + t += x[0]; + } else if (!q.empty() && q.top() > x[0]) { + t += x[0] - q.top(); + q.pop(); + q.push(x[0]); + } + } + return q.size(); + } +}; diff --git a/cpp/2206/CMakeLists.txt b/cpp/2206/CMakeLists.txt index 9321f92..dc1f9a4 100644 --- a/cpp/2206/CMakeLists.txt +++ b/cpp/2206/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2206) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2206 220621.cpp) +ADD_EXECUTABLE(2206 220623.cpp)