Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

826. Most Profit Assigning Work

Medium55.9% Acceptance
ArrayTwo PointersBinary Search
Asked by:
DoorDash
ProblemSolutions (12)VideosCompanies (3)Notes

Problem Statement

You have n jobs and m workers. You are given three arrays: difficulty, profit, and worker where:

  • difficulty[i] and profit[i] are the difficulty and the profit of the ith job, and
  • worker[j] is the ability of jth worker (i.e., the jth worker can only complete a job with difficulty at most worker[j]).

Every worker can be assigned at most one job, but one job can be completed multiple times.

  • For example, if three workers attempt the same job that pays $1, then the total profit will be $3. If a worker cannot complete any job, their profit is $0.

Return the maximum profit we can achieve after assigning the workers to the jobs.

Example 1:

Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
Output: 100
Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get a profit of [20,20,30,30] separately.

Example 2:

Input: difficulty = [85,47,57], profit = [24,66,99], worker = [40,25,25]
Output: 0

Constraints:

  • n == difficulty.length
  • n == profit.length
  • m == worker.length
  • 1 <= n, m <= 104
  • 1 <= difficulty[i], profit[i], worker[i] <= 105
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
D
A
Amazon
N
NetEase

Approach

In #826 Most Profit Assigning Work, each job has a difficulty and a corresponding profit, while each worker has a maximum ability. The goal is to assign workers to jobs they can complete and maximize the total profit. Multiple workers can take the same job.

A common approach is to sort jobs by difficulty and sort workers by ability. Using a two-pointer greedy strategy, iterate through workers and keep track of the best profit available among all jobs with difficulty less than or equal to the worker’s ability. This allows you to efficiently assign the most profitable achievable job to each worker.

Another variant uses binary search on sorted difficulties with a prefix maximum profit array. Both strategies rely on preprocessing the job list so that profit values are non-decreasing with difficulty. The dominant cost comes from sorting the arrays, leading to an overall time complexity of O(n log n + m log m) (or O(n log n + m log n) with binary search) and O(1) to O(n) extra space depending on the implementation.

Complexity

ApproachTime ComplexitySpace Complexity
Sorting + Two Pointers (Greedy)O(n log n + m log m)O(1)
Sorting + Binary Search with Prefix Max ProfitO(n log n + m log n)O(n)

Video Solution Available

codestorywithMIK

View all video solutions

Solutions (12)

Approach 1: Sorting and Greedy Matching

In this approach, we sort both jobs by difficulty and workers by their ability. We then iterate over each worker and find the highest profit job they can perform using a greedy approach.

First, we pair each job's difficulty with its profit and then sort these pairs by difficulty. We also sort the worker array. Next, for each worker, we iterate through the sorted job list and keep track of the maximum profit the worker can obtain, given that their ability must be greater than or equal to the job's difficulty. This ensures that each worker is assigned the most profitable job they can perform, thus maximizing the total profit.

Time Complexity: O(n log n + m log m)
Space Complexity: O(n)

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4int compareJobs(const void *a, const void *b) {




Explanation

The solution first sorts the jobs based on their difficulty and profits, and then sorts the workers based on their ability. It uses a while loop to traverse through eligible jobs for each worker and tracks the maximum profit they can make, which is then added to the total profit.

Approach 2: Efficient Profit Calculation with One Pass

This approach improves efficiency by preparing the job list in advance for profit maximization, and processes each worker in one pass. The basic idea is to preprocess the jobs to track the maximum profit obtainable up to each difficulty level. We create a running maximum profit and apply this to each worker based on their ability directly.

First, jobs are paired and sorted by difficulty; then, as we iterate through them, we constantly update the maximum profit obtainable up to each job's difficulty. When assessing workers, we simply apply their ability to this precomputed list to find the applicable maximum profit, ensuring minimal lookups and passing through the sorted jobs just once.

Time Complexity: O(n log n + m log m)
Space Complexity: O(n)

CC++JavaPythonC#JavaScript





Video Solutions

Watch expert explanations and walkthroughs

Most Profit Assigning Work | Multiple Approaches | With Intuition | Leetcode 826 | codestorywithMIK

codestorywithMIK
37:288,959 views

Asked By Companies

3 companies
D
DoorDash
A
Amazon
N
NetEase

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Container With Most WaterMedium
3SumMedium
3Sum ClosestMedium
4SumMedium
More similar problems

Related Topics

ArrayTwo PointersBinary SearchGreedySorting

Problem Stats

Acceptance Rate55.9%
DifficultyMedium
Companies3

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Can binary search be used in Most Profit Assigning Work?

Yes, after sorting jobs by difficulty, you can precompute the maximum profit up to each difficulty. For each worker, binary search finds the hardest job they can handle, and the stored prefix maximum gives the best profit.

Is Most Profit Assigning Work asked in FAANG interviews?

Yes, variations of this problem appear in coding interviews at large tech companies. It tests understanding of greedy strategies, sorting, and efficient searching techniques, which are common interview themes.

What is the optimal approach for Most Profit Assigning Work?

The optimal approach sorts jobs by difficulty and workers by ability, then uses a greedy two-pointer scan. While iterating through workers, you maintain the maximum profit among jobs they can perform and add it to the total. This avoids checking every job for every worker.

What data structure is best for solving Most Profit Assigning Work?

Arrays combined with sorting are typically sufficient for this problem. A prefix maximum array or a running variable can track the best profit seen so far. Some solutions also use binary search over sorted difficulties for efficient lookups.

5
return
(
(
int
*
)
a
)
[
0
]
-
(
(
int
*
)
b
)
[
0
]
;
6
}
7
8
int
compareWorkers
(
const
void
*
a
,
const
void
*
b
)
{
9
return
(
*
(
int
*
)
a
)
-
(
*
(
int
*
)
b
)
;
10
}
11
12
int
maxProfitAssignment
(
int
*
difficulty
,
int
difficultySize
,
int
*
profit
,
int
profitSize
,
int
*
worker
,
int
workerSize
)
{
13
int
jobs
[
difficultySize
]
[
2
]
;
14
for
(
int
i
=
0
;
i
<
difficultySize
;
i
++
)
{
15
jobs
[
i
]
[
0
]
=
difficulty
[
i
]
;
16
jobs
[
i
]
[
1
]
=
profit
[
i
]
;
17
}
18
qsort
(
jobs
,
difficultySize
,
sizeof
(
jobs
[
0
]
)
,
compareJobs
)
;
19
qsort
(
worker
,
workerSize
,
sizeof
(
int
)
,
compareWorkers
)
;
20
21
int
maxProfit
=
0
,
best
=
0
,
j
=
0
;
22
for
(
int
i
=
0
;
i
<
workerSize
;
i
++
)
{
23
while
(
j
<
difficultySize
&&
worker
[
i
]
>=
jobs
[
j
]
[
0
]
)
{
24
best
=
(
best
>
jobs
[
j
]
[
1
]
)
?
best
:
jobs
[
j
]
[
1
]
;
25
j
++
;
26
}
27
maxProfit
+=
best
;
28
}
29
return
maxProfit
;
30
}
31
32
int
main
(
)
{
33
int
difficulty
[
]
=
{
2
,
4
,
6
,
8
,
10
}
;
34
int
profit
[
]
=
{
10
,
20
,
30
,
40
,
50
}
;
35
int
worker
[
]
=
{
4
,
5
,
6
,
7
}
;
36
int
maxProfit
=
maxProfitAssignment
(
difficulty
,
5
,
profit
,
5
,
worker
,
4
)
;
37
printf
(
"Max Profit: %d\n"
,
maxProfit
)
;
38
return
0
;
39
}
1
#
include
<stdio.h>
2
#
include
<stdlib.h>
3
4
int
compare
(
const
void
*
a
,
const
void
*
b
)
{
5
return
(
(
int
*
)
a
)
[
0
]
-
(
(
int
*
)
b
)
[
0
]
;
6
}
7
8
int
maxProfitAssignment
(
int
*
difficulty
,
int
difficultySize
,
int
*
profit
,
int
profitSize
,
int
*
worker
,
int
workerSize
)
{
9
int
jobs
[
difficultySize
]
[
2
]
;
10
for
(
int
i
=
0
;
i
<
difficultySize
;
i
++
)
{
11
jobs
[
i
]
[
0
]
=
difficulty
[
i
]
;
12
jobs
[
i
]
[
1
]
=
profit
[
i
]
;
13
}
14
qsort
(
jobs
,
difficultySize
,
sizeof
(
jobs
[
0
]
)
,
compare
)
;
15
qsort
(
worker
,
workerSize
,
sizeof
(
int
)
,
compare
)
;
16
17
int
maxProfit
=
0
,
best
=
0
,
j
=
0
;
18
for
(
int
i
=
0
;
i
<
difficultySize
;
i
++
)
{
19
if
(
jobs
[
i
]
[
1
]
>
best
)
{
20
best
=
jobs
[
i
]
[
1
]
;
21
}
22
jobs
[
i
]
[
1
]
=
best
;
// Store max profit up to this difficulty
23
}
24
25
for
(
int
i
=
0
,
j
=
0
;
i
<
workerSize
;
i
++
)
{
26
while
(
j
<
difficultySize
&&
worker
[
i
]
>=
jobs
[
j
]
[
0
]
)
{
27
j
++
;
28
}
29
if
(
j
>
0
)
{
30
maxProfit
+=
jobs
[
j
-
1
]
[
1
]
;
31
}
32
}
33
return
maxProfit
;
34
}
35
36
int
main
(
)
{
37
int
difficulty
[
]
=
{
2
,
4
,
6
,
8
,
10
}
;
38
int
profit
[
]
=
{
10
,
20
,
30
,
40
,
50
}
;
39
int
worker
[
]
=
{
4
,
5
,
6
,
7
}
;
40
int
maxProfit
=
maxProfitAssignment
(
difficulty
,
5
,
profit
,
5
,
worker
,
4
)
;
41
printf
(
"Max Profit: %d\n"
,
maxProfit
)
;
42
return
0
;
43
}

Explanation

This C implementation sorts jobs and workers, and computes a running maximum profit for each difficulty level. The logic uses these precomputed profits to quickly sum the maximum applicable profits for each worker in a single iteration.