Funcia
Lv.3greedyheapsorting

Job Sequencing with Deadlines

Memory limit
256 MB
Correctness time limit
10000 ms
Solvers
0
0

Job i takes one day and pays profits[i] if finished on or before day deadlines[i]. One job per day, starting on day 1. Return the maximum total profit.

Examples

deadlinesprofitsresult
[2, 1, 2, 1, 3][100, 19, 27, 25, 15]142
[1, 1, 1][5, 9, 2]9
[1000000000][7]7

Efficiency

This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.

Function signature

solution(deadlines: int[], profits: int[]) → long

Constraints

  • 1≤n≤200,0001 \le n \le 200{,}000, 1≤1 \le deadlines[i], profits[i] ≤109\le 10^9; the answer can exceed 32 bits.

Expected complexity

Time
O(N log N)
Space
O(N)

Examples

deadlinesprofitsExpected
[2,1,2,1,3][100,19,27,25,15]142
[1,1,1][5,9,2]9
[1000000000][7]7

Hint

Hint

Process jobs by deadline, keep the chosen profits in a min-heap and drop the smallest whenever the heap outgrows the current deadline; sum what remains. Searching day by day for the latest free slot is O(N2)O(N^2).

Time limits per language

LanguageEfficiency time limit
C (GCC, gnu17)200 msestimated
C++ (GCC, gnu++23)200 msderived
Python 3.131000 msderived
Java 251268 msderived
Kotlin 2.4 (JVM)1400 msestimated
C# 14 (.NET 10)1400 msestimated

Test groups

GroupWeightTestsVisibility
sample03inputs and outputs
correctness408verdict only
performance604verdict only
Loading editor…
Log in to run or submit code.

No result yet. Run or submit your code.