You are given two independent coding problems.
You are planning to take a set of courses and are given prerequisite relationships between them.
n
courses labeled from
0
to
n - 1
.
prerequisites
, where each pair
[a, b]
means you must complete course
b
before you can take course
a
.
Determine whether it is possible to finish all n courses.
true
if you can complete all courses (i.e., the prerequisite graph has no cycle); otherwise return
false
.
You should specify the time and space complexity of your algorithm.
Assume:
If it is possible to finish all courses, compute the minimum number of semesters required to complete all n courses. If it is impossible (due to cycles), indicate that it cannot be done.
Describe an efficient algorithm for this and its time and space complexity.
You may assume:
1 <= n <= 10^5
.
0 <= len(prerequisites) <= 2 * 10^5
.
You are running a social media website that suddenly went viral. To protect your backend, you added a rate limiter. Now you want to analyze logs to determine which requests would be dropped by this limiter.
You are given:
requests
, where the
index
is the request ID (0-based), and the
value
requests[i]
is the time in seconds when the
i
-th request arrived.
requests
is non-decreasing (requests are logged in arrival order; multiple requests can have the same timestamp if they arrive in the same second).
Your rate limiter enforces the following rules:
Formally:
t
, among all requests with arrival time exactly
t
, at most 3 can be processed; any additional requests arriving at time
t
must be dropped.
t
, consider all requests with arrival times in the interval
[t - 9, t]
(inclusive). If processing this request would cause the
total number of processed requests
in that interval to exceed 20, then this request must be dropped.
Rules are applied in arrival order:
Given the array requests, return a list of the times of all dropped requests, in the order they appear in the input.
You should:
10^5
requests.
You may assume:
1 <= len(requests) <= 10^5
.
0 <= requests[i] <= 10^9
.
requests
is non-decreasing.