Skip to content

Commit 7fc8382

Browse files
committed
change logic by implementing break once a scenario is already worse than best
1 parent 12da74e commit 7fc8382

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

implement-laptop-allocation/main.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,16 @@ def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person
4444
for person, laptop in zip(people, scenario):
4545
total_sadness += sadness(person, laptop)
4646

47-
#Check if this is the best scenario so far
47+
#Implement early break if total is already worse than best
48+
if total_sadness >= best_total_sadness:
49+
break
50+
51+
# Check if this scenario is better
4852
if total_sadness < best_total_sadness:
49-
best_total_sadness = total_sadness
50-
best_scenario_assignment = scenario
53+
best_total_sadness = total_sadness
54+
best_scenario_assignment = scenario
5155

56+
5257
# Convert best_scenario_assignment into Dict[Person, Laptop]
5358
return {person: laptop for person, laptop in zip(people, best_scenario_assignment)}
5459

0 commit comments

Comments
 (0)