#🔒 Trouble with Hackerrank Greedy Problem

6 messages · Page 1 of 1 (latest)

dreamy cedar
#
from heapq import heapify, heappop, heappush
from collections import Counter
from math import inf


def calculateMinimumTimeUnits(tasks, m, k):
    
    counter = Counter(tasks)
    
    maxHeap = [(-count, task_type) for task_type, count in counter.items()]
    heapify(maxHeap)

    idle_info = [{} for _ in range(m)]
    
    time = 0
    
    while maxHeap:
        
        time += 1
        scheduled = False
            
        for machine_id in range(m):

            if not maxHeap:
                break
            
            found = False
            temp = []
            
            while maxHeap and not found:
                
                neg_count, task_type = heappop(maxHeap)
                
                if task_type not in idle_info[machine_id] or \
                idle_info[machine_id][task_type] <= time:
                    
                    scheduled = True
                    found = True
                    
                    if neg_count != -1:
                        heappush(maxHeap, (neg_count + 1, task_type))
                        idle_info[machine_id][task_type] = time + k + 1
                        
                else:
                    temp.append((neg_count, task_type))
            
            for item in temp:
                heappush(maxHeap, item)
            
        if not scheduled and maxHeap:

            min_time = inf
            
            for _, task_type in maxHeap:
                for machine_id in range(m):
                    if task_type in idle_info[machine_id] \
                    and idle_info[machine_id][task_type] >= time:
                        candidate_time = idle_info[machine_id][task_type]
                        min_time = min(min_time, candidate_time)
            
            if min_time == inf:
                min_time = time + 1
            
            time = min_time - 1
        
    
    return time
ornate sigilBOT
#

@dreamy cedar

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.

#

Hey @dreamy cedar!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
dreamy cedar
#

Having trouble with this hackerrank problem: https://www.hackerrank.com/contests/software-engineer-prep-kit/challenges/task-scheduler-cooldown-multiple-machines/problem

This code fails 12 out of the 15 tests and I can't see the inputs of the failed tests. I tried generating tests through LLMs. Any advice on what could be wrong or coming up with good test cases:

ornate sigilBOT
#

@dreamy cedar

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.