#πŸ”’ Design Task Manager

100 messages Β· Page 1 of 1 (latest)

errant plume
#

Leetcode 3408 question

class TaskManager:

    def __init__(self, tasks: List[List[int]]):
        self.dict1=[]
        self.task=tasks[:]
        for i,j,k in self.task:
            heapq.heappush(self.dict1,(-k,-j))
        self.dict3={} 
        for i,j,k in self.task:
            self.dict3[j]=k
        self.dict2={}
        for i,j,k in self.task:
            self.dict2[j]=i
        self.set1=set()
          

    def add(self, userId: int, taskId: int, priority: int) -> None:
        heapq.heappush(self.dict1,(-priority,-taskId))
        self.dict3[taskId]=priority
        self.dict2[taskId]=userId
        if taskId in self.set1:
            self.set1.discard(taskId)
        

    def edit(self, taskId: int, newPriority: int) -> None:
        heapq.heappush(self.dict1,(-newPriority,-taskId))
        self.dict3[taskId]=newPriority

    def rmv(self, taskId: int) -> None:
        self.set1.add(taskId)
      

    def execTop(self) -> int:
        if not self.dict1:
            return -1
        (x,y)=heapq.heappop(self.dict1)
        while -y in self.set1 or self.dict3[-y]!=-x:
            if not self.dict1:
                return -1
            (x,y)=heapq.heappop(self.dict1)
        # heapq.heappush(self.dict1,(x,y))
        return self.dict2[-y]
         
# Your TaskManager object will be instantiated and called as such:
# obj = TaskManager(tasks)
# obj.add(userId,taskId,priority)
# obj.edit(taskId,newPriority)
# obj.rmv(taskId)
# param_4 = obj.execTop()
vocal gladeBOT
#

@errant plume

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hazy crescent
#

hi there

#

whats the issue? And how are you trying to solve it? Your variable naming makes it very heard to understand your solution

errant plume
#

i just try to add the tasks into a maxheap called "dict1" that has priority com taskid

#

for tracking of removed tasks i made a set

red horizon
#

yeah your variable naming is really not very good. why are you naming your variables dict1, dict2, dict3 ? not to mention dict1 is not even a dictionary, but a list...

errant plume
#

yeah my bad

#

let me telll you my approach did you understand the question

hazy crescent
#

yes

#

ik the question, i spent like 2 hrs yesterday on it

#

lol

errant plume
#

got it?

hazy crescent
#

tell me your approach

errant plume
#

i just add everything into maxheap while exceuting i just check whether its already removed or its value is not correct like that

hazy crescent
#

yes go on, how are you implementing the init method?

errant plume
#

just initializing the maxheap with the current tasks available

#

and also assigning dictionaries to the taskid and their priority

hazy crescent
#

you insert into the heapq like this (negPriority, negTaskId) right?

errant plume
#

yeah

hazy crescent
errant plume
#

-priority,-taskid

hazy crescent
#

ok

errant plume
hazy crescent
#

lets call the heapq as self.pq instead of dict1 shall we? it will help us avoid confusion

#

and the taskId-priority dict as taskPriorityMap

#

or taskPriorityDict whatever

#

then what do you do in the init method?

errant plume
#

and also taskid:userid for returning userid

hazy crescent
#

cool

#

what is that set?

errant plume
#

that stores which tasks has removed

hazy crescent
#

hmm I see trouble there

errant plume
#
class TaskManager:

    def __init__(self, tasks: List[List[int]]):
        self.pq=[]
        self.task=tasks[:]
        for i,j,k in self.task:
            heapq.heappush(self.pq,(-k,-j))
        self.taskPriorityMap={} 
        for i,j,k in self.task:
            self.taskPriorityMap[j]=k
        self.taskUserMap={}
        for i,j,k in self.task:
            self.taskUserMap[j]=i
        self.removedtask=set()
          

    def add(self, userId: int, taskId: int, priority: int) -> None:
        heapq.heappush(self.pq,(-priority,-taskId))
        self.taskPriorityMap[taskId]=priority
        self.taskUserMap[taskId]=userId
        if taskId in self.removedtask:
            self.removedtask.discard(taskId)
        

    def edit(self, taskId: int, newPriority: int) -> None:
        heapq.heappush(self.pq,(-newPriority,-taskId))
        self.taskPriorityMap[taskId]=newPriority

    def rmv(self, taskId: int) -> None:
        self.removedtask.add(taskId)
      

    def execTop(self) -> int:
        if not self.pq:
            return -1
        (x,y)=heapq.heappop(self.pq)
        while -y in self.removedtask or self.taskPriorityMap[-y]!=-x:
            if not self.pq:
                return -1
            (x,y)=heapq.heappop(self.pq)
        # heapq.heappush(self.pq,(x,y))
        return self.taskUserMap[-y]
         
# Your TaskManager object will be instantiated and called as such:
# obj = TaskManager(tasks)
# obj.add(userId,taskId,priority)
# obj.edit(taskId,newPriority)
# obj.rmv(taskId)
# param_4 = obj.execTop()
hazy crescent
#

now your code is much more readable

errant plume
#

what was the problem can you find out please

hazy crescent
#

brb

errant plume
#

?

hazy crescent
#

im back

#

brb - be right back

#

your init and add methods look solid

#

hmm edit method looks correct too

red horizon
hazy crescent
#

cos i like camelcase, its easier to type

red horizon
#

it's not PEP compliant though

hazy crescent
#

i dont give a F though xD

red horizon
#

if you write camelcase variable names in python it's just gonna be an ugly mix because 99% of the libraries you use will have method names and variables named with snake case. i just don't think this is a good habit to pass on to other people

hazy crescent
#

true that

#

old habbits die hard

#

the problem is in the execTop method prolly

red horizon
#

rename the variables i, j, k in __init__ and x, y in execTop (which should be exec_top) to be descriptive too

hazy crescent
#

yeah that and the init method could be rewritten as:

        self.pq = []
        self.taskUserMap = {}
        self.taskPriorityMap = {}

        for task in tasks:
            self.pq.append((-task[2], -task[1]))
            self.taskUserMap[task[1]] = task[0]
            self.taskPriorityMap[task[1]] = task[2]
        heapq.heapify(self.pq)
        self.removedtask = set()
errant plume
#

yeah all look good whats the proble,

red horizon
#

use snake_case instead of camelCase for python variable names and functions:
task_user_map instead of taskUserMap etc.
otherwise you're just going to set yourself up for pain later

#

if you ever want to collaborate with other people etc.

hazy crescent
#

+1

errant plume
#

yeah

#

this is painful than that

hazy crescent
#

i think i found the issue

#

im not sure

errant plume
#

yeah say it

hazy crescent
#

ohh ok

#

got it

#

@errant plume

#

you are not adding the executed task to the removed set after popping it from the heap

errant plume
#

one sec

hazy crescent
#

lmk if it works

errant plume
#

got it tq

hazy crescent
#

hell yeah

#

gg

errant plume
#

how did you solve ?

hazy crescent
#

i tried to maintain a single pq first like this (-P, -tId, uId)

#

did not work obv

errant plume
#

finally you got ?

#

other than my approach

hazy crescent
#

then I did something similar to what you have done

#

except I did not maintain a removed set

#

I just did self.taskPriorityMap[taskId] = None for removed tasks

#

and my exec method is diff:

    def execTop(self) -> int:
        while self.pq:
            topTask = heapq.heappop(self.pq)
            if self.taskPriorityMap[-topTask[1]] == -topTask[0]:
                self.taskPriorityMap[-topTask[1]] = None
                return self.taskUserMap[-topTask[1]]
        return -1```
errant plume
#

yeah got it

hazy crescent
#

took me hours

#

very interesting problem though

#

from next sol onwards, try to name your vars properly

errant plume
#

yeah tq

#

did todays contest

#

?

hazy crescent
#

not yet, its a hard one

errant plume
#

3rd problem is little bit hard ngl backtracking

hazy crescent
#

idk backtracking :(

errant plume
#

where are you from

hazy crescent
#

TN

#

wbu

errant plume
#

chennao

hazy crescent
#

dm

vocal gladeBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.