Workspace Job Sequencing
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Job Sequencing visualizer

Learn Job Sequencing with a step-by-step Greedy & Intervals visualizer, JavaScript source code and a worked example. Practice scheduling and resource allocation.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 6Execution step
Reference example · JavaScript
Inspect reference input
[
  [
    1,
    2,
    100
  ],
  [
    2,
    1,
    19
  ],
  [
    3,
    2,
    27
  ],
  [
    4,
    1,
    25
  ],
  [
    5,
    1,
    15
  ]
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
a.sort((a,b)=>b[2]-a[2]);
let slots=Array(Math.max(...a.map(x=>x[1]))).fill(null),profit=0;
for(let [id,d,p] of a){
for(let i=d-1;
i>=0;
i--)if(slots[i]===null){
slots[i]=id;
profit+=p;
break
}
s("Assign latest available slot",{
Jobs:a,Slots:slots,Profit:[profit]
})
}
return [slots.filter(x=>x!==null).length,profit]
};

Execution canvas

Active Changed
STEP 1

Assign latest available slot

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Jobs5 rows
Slots2 items
Profit1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Greedy & Intervals · Scheduling and resource allocation

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

[
  [
    1,
    2,
    100
  ],
  [
    2,
    1,
    19
  ],
  [
    3,
    2,
    27
  ],
  [
    4,
    1,
    25
  ],
  [
    5,
    1,
    15
  ]
]

Execution steps

  1. Assign latest available slot

Computed output

[
  2,
  127
]
JavaScript reference implementation
const solve = (a,s)=>{
  a.sort((a,b)=>b[2]-a[2]);
  let slots=Array(Math.max(...a.map(x=>x[1]))).fill(null),profit=0;
  for(let [id,d,p] of a){
    for(let i=d-1;
    i>=0;
    i--)if(slots[i]===null){
      slots[i]=id;
      profit+=p;
      break
    }
    s("Assign latest available slot",{
      Jobs:a,Slots:slots,Profit:[profit]
    })
  }
  return [slots.filter(x=>x!==null).length,profit]
};

Continue learning

Practice related DSA questions in the same topic.