class PowerInterval {
public start: number;
public end: number;
public power: number;
constructor(start:number, end: number, power: number) {
this.start = start;
this.end = end;
this.power = power;
}
}
/**
*
* @param jobsUnsorted
*/
const maxPower = (jobsUnsorted: Array<PowerInterval>): number => {
const jobs = jobsUnsorted.sort((a, b) => a.start - b.start);
const length = jobs.length;
let start = jobs[0].start;
let end = jobs[0].end;
let max = jobs[0].power;
for (let index = 1; index < length; index += 1) {
const previous = jobs[index - 1];
const current = jobs[index];
if (current.start < end) {
// overlap
end = Math.max(end, current.end)
max += current.power;
} else {
max = Math.max(max, current.power);
}
}
return max;
}
Time Complexity O(N)
Any thoughts?