-
Notifications
You must be signed in to change notification settings - Fork 243
Gallery Wall
kyra-ptn edited this page Aug 29, 2024
·
2 revisions
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Q:
- What is the desired outcome?
- To organize a collection of art prints into a 2D array where each row contains distinct strings and the number of rows is minimal.
- What is the desired outcome?
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Count the occurrences of each print, then distribute them into the minimal number of rows such that each row contains distinct prints.
1) Count the occurrences of each print.
2) Organize prints into rows by their frequency.
3) Initialize the 2D array `rows`.
4) Distribute prints into rows while ensuring each row contains distinct prints.
5) Remove any empty rows and return the result.
- Not correctly distributing prints into rows, leading to duplicate prints in a row.
def organize_exhibition(collection):
# Step 1: Manually count occurrences of each print
print_count = {}
for print_name in collection:
if print_name in print_count:
print_count[print_name] += 1
else:
print_count[print_name] = 1
# Step 2: Create a dictionary of unique prints grouped by their counts
unique_prints = {}
for print_name, count in print_count.items():
if count in unique_prints:
unique_prints[count].append(print_name)
else:
unique_prints[count] = [print_name]
# Step 3: Determine the number of rows needed
max_count = max(print_count.values())
# Step 4: Initialize the 2D array (rows) to store the organized prints
rows = [[] for _ in range(max_count)]
# Step 5: Distribute prints into rows
for count, prints in unique_prints.items():
for i in range(count):
for print_name in prints:
rows[i].append(print_name)
# Step 6: Remove any empty rows
result = [row for row in rows if row]
return result