Workspace Non Overlapping Intervals
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Non Overlapping Intervals visualizer

Learn Non Overlapping Intervals 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 / 5Execution step
Reference example · JavaScript
Inspect reference input
[
  [
    1,
    2
  ],
  [
    2,
    3
  ],
  [
    3,
    4
  ],
  [
    1,
    3
  ]
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
a.sort((a,b)=>a[1]-b[1]);
let end=-Infinity,c=0;
for(let x of a){
if(x[0]>=end){
end=x[1];
c++
}
s("Keep interval with earliest finish",{
Intervals:a,Kept:[c],End:[end]
})
}
return a.length-c
};

Execution canvas

Active Changed
STEP 1

Keep interval with earliest finish

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

Intervals4 rows
Kept1 items
End1 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
  ],
  [
    2,
    3
  ],
  [
    3,
    4
  ],
  [
    1,
    3
  ]
]

Execution steps

  1. Keep interval with earliest finish

Computed output

1
JavaScript reference implementation
const solve = (a,s)=>{
  a.sort((a,b)=>a[1]-b[1]);
  let end=-Infinity,c=0;
  for(let x of a){
    if(x[0]>=end){
      end=x[1];
      c++
    }
    s("Keep interval with earliest finish",{
      Intervals:a,Kept:[c],End:[end]
    })
  }
  return a.length-c
};

Continue learning

Practice related DSA questions in the same topic.