Skip to content

Commit 814e8c9

Browse files
committed
improve efficiency by breaking the loop when a perfect match (sadness = 0) is found
1 parent c51ceee commit 814e8c9

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Python
2+
__pycache__/
3+
.mypy_cache/
4+
.venv/
5+
6+
# Node
7+
node_modules/
8+
9+
# Mac
10+
.DS_Store

implement_laptop_allocation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person
5151
if sadness < best_sadness:
5252
best_sadness = sadness
5353
best_laptop = laptop
54+
55+
# If sadness is 0, it means this is the perfect match, no need to check further
56+
if sadness == 0:
57+
break
5458

5559
# assign and remove from pool
5660
if best_laptop:

0 commit comments

Comments
 (0)