TIP102 Unit1 Delete Minimum - codepath/compsci_guides GitHub Wiki
TIP102 Unit 1 Session 2 Standard (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10 mins
- 🛠️ Topics: Arrays, Iteration, While Loop
U-nderstand
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
- The function
delete_minimum_elements()
should take a list of integers and continuously remove the minimum element until the list is empty, returning a new list of the removed elements in order.
HAPPY CASE
Input: [5, 3, 2, 4, 1]
Expected Output: [1, 2, 3, 4, 5]
Input: [5, 2, 1, 8, 2]
Expected Output: [1, 2, 2, 5, 8]
EDGE CASE
Input: [1, 1, 1, 1]
Expected Output: [1, 1, 1, 1]
Input: []
Expected Output: []
P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Define a function that uses a while loop to continuously find and remove the minimum element from the list, appending each removed element to a new list until the original list is empty.
1. Define the function `delete_minimum_elements(hunny_jar_sizes)`.
2. Initialize an empty list `removed_elements` to store the removed elements.
3. Use a while loop to iterate as long as `hunny_jar_sizes` is not empty.
4. Within the loop:
a. Find the minimum element in `hunny_jar_sizes`.
b. Append this minimum element to `removed_elements`.
c. Remove the minimum element from `hunny_jar_sizes`.
5. Return the `removed_elements` list.
⚠️ Common Mistakes
- Forgetting to remove the minimum element from the original list.
- Not handling empty lists correctly.
I-mplement
Implement the code to solve the algorithm.
def delete_minimum_elements(hunny_jar_sizes):
removed_elements = []
while hunny_jar_sizes:
min_element = min(hunny_jar_sizes)
removed_elements.append(min_element)
hunny_jar_sizes.remove(min_element)
return removed_elements