Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
much better solution . no heap!

Mohammed Dh Abbas

Jul 16, 2024

from heapq import * #class job: # def __init__(self, start, end, cpu_load): # self.start = start # self.end = end # self.cpuLoad = cpu_load # You can override/modify __lt__ as per your need #setattr(Job, "__lt__", lambda self, other: write logic here) class Solution: def findMaxCPULoad(self, jobs): max_cpu_load = 0 sum = 0 jobs.sort(key=lambda x: x.start) def overlap(a, b): return b.start < a.end for i in range(len(jobs)): if i == 0 or not overlap(jobs[i-1], jobs[i]): sum = jobs[i].cpuLoad else: sum += jobs[i].cpuLoad max_cpu_load = max(max_cpu_load, sum) return max_cpu_load

0

0

Comments
Comments
hxue1998
hxue1998 a year ago

There's a bug in this solution if we consider the case [[1, 6, 1], [2, 3, 2], [4, 5, 3]].

Roman Kishchenko
Roman Kishchenko9 months ago

This will not work for all cases because jobs[i - 1] might not overlap while jobs[i - 2] still overlaps. For example, it will not work for [[1, 9, 3], [2, 5, 4], [7, 9, 6]]

On this page