You are given three independent coding problems.
You have n independent tasks that must be split between two workers (worker 1 and worker 2). Every task must be done by exactly one of the two workers.
For each task (i) (0-indexed or 1-indexed, as you prefer), you are given:
reward1[i]
: the reward if
worker 1
does task (i)
reward2[i]
: the reward if
worker 2
does task (i)
Additionally, worker 1 must do exactly k tasks in total (where (0 \le k \le n)). The remaining (n - k) tasks must be done by worker 2.
Task:
Input format (conceptual):
n
— number of tasks.
k
— number of tasks that must be assigned to worker 1.
reward1
of length
n
.
reward2
of length
n
.
Output:
You should design an algorithm that works efficiently for large (n) (e.g., up to (10^5) or (10^6)).
You are given a directed graph with n nodes labeled from 1 to n and n - 1 directed edges. It is guaranteed that if you ignore edge directions, the underlying undirected graph is a tree (i.e., it is connected and acyclic). However, the current directions of the edges may be arbitrary, so the graph may not currently have a single node reachable from all others.
You are allowed to reverse the direction of any subset of edges. Reversing an edge counts as one operation.
You want to make the graph such that there exists some node r (1 ≤ r ≤ n) with the following property:
x
(including
r
), there is a
directed path
from
x
to
r
following the directions of the (possibly reversed) edges.
In other words, after reorienting some edges, all nodes should be able to "flow" to a single root node r along directed paths.
Task:
r
, determine the
minimum number of edge reversals
needed to achieve the property that every node has a directed path to
r
.
Input format (conceptual):
n
— number of nodes.
n - 1
directed edges, each given as an ordered pair
(u, v)
meaning there is an edge from
u
to
v
.
Output:
r
, every node has a directed path to
r
.
r
that achieves this minimum.)
Your algorithm should run in time close to linear in n (e.g., (O(n)) or (O(n \log n))).
You are given a binary matrix A of size m × n ((m) rows, (n) columns). Each cell is either 0 or 1. Each row is sorted in non-decreasing order, meaning:
0 0 0 1 1 1
(possibly all 0s or all 1s).
Task:
1
in any row
.
-1
.
You may assume 0-based column indexing (return an integer in [0, n-1]) or 1-based indexing (return in [1, n]); just be clear and consistent in your implementation.
Input format (conceptual):
m
,
n
— number of rows and columns.
m × n
matrix
A
of 0s and 1s, where each row is sorted: all 0s, then all 1s.
Output:
1
, or
-1
if no such column exists.
Complexity requirement: