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 many stones can there be on the plane?
Could there be no stones?
What if no stones can be removed?
HAPPY CASE
Input: stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
Output: 5
EDGE CASE
Input: stones = [[0,0]]
Output: 0
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, some things we want to consider are:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We can first create an empty disjoint set, add each stone in a loop, and at each iteration compute the number of sets. At the end, # of stones - # of sets.
1) Create a disjoint set. 2) For each stone a) Add the coordinate x, ~y (i.e. -(y-1)) to the set b) Update number of sets 3) Return number of stones - number of sets
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Solution(object):
def removeStones(self, stones):
stones = list(map(tuple, stones))
s = set(stones)
d = collections.defaultdict(list)
for i,j in s:
d[i].append(j)
d[j].append(i)
def dfs(i,j):
for y in d[i]: # find all points in x=i
if (i,y) not in s: continue
s.remove((i,y))
dfs(i,y)
for x in d[j]: # find all points in y=j
if (x,j) not in s: continue
s.remove((x,j))
dfs(x,j)
n = len(s)
res = 0
for i,j in stones:
if (i,j) not in s: continue
s.remove((i,j))
dfs(i,j)
# n-len(s) represent the length of graph, e.g. the number of element removed through dfs
res += n - len(s) - 1
n = len(s)
return res
class Solution {
// return true if stone a and b shares row or column
boolean shareSameRowOrColumn(int[] a, int[] b) {
return a[0] == b[0] || a[1] == b[1];
}
void dfs(int[][] stones, List<Integer>[] adj, int[] visited, int src) {
// mark the stone as visited
visited[src] = 1;
// iterate over the adjacent, and iterate over it if not visited yet
for (int adjacent : adj[src]) {
if (visited[adjacent] == 0) {
dfs(stones, adj, visited, adjacent);
}
}
}
int removeStones(int[][] stones) {
// create adjacency list to store edges
List<Integer>[] adj = new ArrayList[stones.length];
for (int i = 0; i < stones.length; i++) {
adj[i] = new ArrayList<>();
}
for (int i = 0; i < stones.length; i++) {
for (int j = i + 1; j < stones.length; j++) {
if (shareSameRowOrColumn(stones[i], stones[j])) {
adj[i].add(j);
adj[j].add(i);
}
}
}
// create array to mark visited stones
int[] visited = new int[stones.length];
// counter for connected components
int componentCount = 0;
for (int i = 0; i < stones.length; i++) {
if (visited[i] == 0) {
// If the stone is not visited yet,
// Start the DFS and increment the counter
componentCount++;
dfs(stones, adj, visited, i);
}
}
// return the maximum stone that can be removed
return stones.length - componentCount;
}
};
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Time complexity: O(N^2 + E) Building the graph will need O(N^2) as needed to traverse over all pairs of stones. During the DFS traversal, each stone only is visited once. This is because we mark each stone as visited as soon as we see it, and then we only visit stones that are not marked as visited. In addition, when we iterate over the edge list of each stone, we look at each edge once.
Space complexity: O(N + E) Building the adjacency list will take O(E) space. To keep track of visited vertices, an array of size O(N), is required. Also, the run-time stack for DFS will use O(N) space.