-
Notifications
You must be signed in to change notification settings - Fork 248
The Earliest Moment When Everyone Become Friends
- 🔗 Leetcode Link: https://leetcode.com/problems/the-earliest-moment-when-everyone-become-friends/
- 💡 Problem Difficulty: Medium
- ⏰ Time to complete: __ mins
- 🛠️ Topics: Graphs, Union Find
- 🗒️ Similar Questions: Number of Provinces
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?
- How can we sort the log items?
- Sort the log items by their timestamp.
- How can we model this problem as a graph problem?
- To keep track of connectivity of each element in the subset or connectivity of subsets with each other, you can use Union Find data structure to do this operation efficiently.
HAPPY CASE
Input:
Output:
Input:
Output:
EDGE CASE
Input:
Output:
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For graph problems, we want to consider the following approaches:
- Union Find: Let's use a union-find data structure. At the beginning we have a graph with N nodes but no edges. Then we loop through the events and if unite each node until the number of connected components reach to 1. Notice that each time two different connected components are united the number of connected components decreases by 1.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Write a 1-2 setence overview that explains the general approach.
1.
2.
- What are some common pitfalls students might have when implementing this solution?
Implement the code to solve the algorithm.
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Trace through your code with an input to check for the expected output
- Catch possible edge cases and off-by-one errors
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of XXX.
-
Time Complexity:
O(N)
because ... -
Space Complexity:
O(N)
because ...