Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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, andworker[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.
$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.lengthn == profit.lengthm == worker.length1 <= n, m <= 1041 <= difficulty[i], profit[i], worker[i] <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Binary Search · Greedy
[2,4,6,8,10] [10,20,30,40,50] [4,5,6,7]
[85,47,57] [24,66,99] [40,25,25]
maximum-number-of-tasks-you-can-assign)successful-pairs-of-spells-and-potions)maximum-matching-of-players-with-trainers)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #826: Most Profit Assigning Work
class Solution {
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
Arrays.sort(worker);
int n = profit.length;
int[][] jobs = new int[n][0];
for (int i = 0; i < n; ++i) {
jobs[i] = new int[] {difficulty[i], profit[i]};
}
Arrays.sort(jobs, (a, b) -> a[0] - b[0]);
int ans = 0, mx = 0, i = 0;
for (int w : worker) {
while (i < n && jobs[i][0] <= w) {
mx = Math.max(mx, jobs[i++][1]);
}
ans += mx;
}
return ans;
}
}
// Accepted solution for LeetCode #826: Most Profit Assigning Work
func maxProfitAssignment(difficulty []int, profit []int, worker []int) (ans int) {
sort.Ints(worker)
n := len(profit)
jobs := make([][2]int, n)
for i, p := range profit {
jobs[i] = [2]int{difficulty[i], p}
}
sort.Slice(jobs, func(i, j int) bool { return jobs[i][0] < jobs[j][0] })
mx, i := 0, 0
for _, w := range worker {
for ; i < n && jobs[i][0] <= w; i++ {
mx = max(mx, jobs[i][1])
}
ans += mx
}
return
}
# Accepted solution for LeetCode #826: Most Profit Assigning Work
class Solution:
def maxProfitAssignment(
self, difficulty: List[int], profit: List[int], worker: List[int]
) -> int:
worker.sort()
jobs = sorted(zip(difficulty, profit))
ans = mx = i = 0
for w in worker:
while i < len(jobs) and jobs[i][0] <= w:
mx = max(mx, jobs[i][1])
i += 1
ans += mx
return ans
// Accepted solution for LeetCode #826: Most Profit Assigning Work
struct Solution;
use std::collections::BTreeMap;
impl Solution {
fn max_profit_assignment(difficulty: Vec<i32>, profit: Vec<i32>, worker: Vec<i32>) -> i32 {
let n = difficulty.len();
let mut btm: BTreeMap<i32, i32> = BTreeMap::new();
for i in 0..n {
let v = btm.entry(difficulty[i]).or_default();
*v = profit[i].max(*v);
}
let mut prev = 0;
for (_, v) in btm.iter_mut() {
if prev > *v {
*v = prev;
}
prev = *v;
}
let mut res = 0;
for w in worker {
res += *btm.range(0..=w).rev().map(|(_, v)| v).next().unwrap_or(&0);
}
res
}
}
#[test]
fn test() {
let difficulty = vec![2, 4, 6, 8, 10];
let profit = vec![10, 20, 30, 40, 50];
let worker = vec![4, 5, 6, 7];
let res = 100;
assert_eq!(
Solution::max_profit_assignment(difficulty, profit, worker),
res
);
}
// Accepted solution for LeetCode #826: Most Profit Assigning Work
function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number {
const n = profit.length;
worker.sort((a, b) => a - b);
const jobs = Array.from({ length: n }, (_, i) => [difficulty[i], profit[i]]);
jobs.sort((a, b) => a[0] - b[0]);
let [ans, mx, i] = [0, 0, 0];
for (const w of worker) {
while (i < n && jobs[i][0] <= w) {
mx = Math.max(mx, jobs[i++][1]);
}
ans += mx;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.