Approach
Breadth-first search
For Maximum Profit in Job Scheduling, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 36 lines of Java from the credited upstream file 1235-3.java.
- The implementation visibly relies on sequence storage, work queue.
- 5 loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {3 final int n = startTime.length;4 Job[] jobs = new Job[n];5 6 for (int i = 0; i < n; ++i)7 jobs[i] = new Job(startTime[i], endTime[i], profit[i]);8 9 Arrays.sort(jobs, Comparator.comparingInt(Job::startTime));10 11 12 for (int i = 0; i < n; ++i)13 startTime[i] = jobs[i].startTime;14 15 return getMaxProfit(jobs);16 }17 18 private record Job(int startTime, int endTime, int profit) {}19 20 private int getMaxProfit(Job[] jobs) {21 int maxProfit = 0;22 Queue<Job> minHeap = new PriorityQueue<>(Comparator.comparingInt(Job::endTime));23 24 for (Job job : jobs) {25 while (!minHeap.isEmpty() && job.startTime >= minHeap.peek().endTime)26 maxProfit = Math.max(maxProfit, minHeap.poll().profit);27 minHeap.offer(new Job(job.startTime, job.endTime, job.profit + maxProfit));28 }29 30 while (!minHeap.isEmpty())31 maxProfit = Math.max(maxProfit, minHeap.poll().profit);32 33 return maxProfit;34 }35}36