LeetCode 1949: Strong Friendship Solution
Master LeetCode problem 1949 (Strong Friendship), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1949. Strong Friendship
Problem Explanation
Explanation:
To solve this problem, we can use the concept of graph traversal. We will represent the friendships as an undirected graph where each friend is a node and each friendship is an edge connecting two nodes. Then, we can perform a depth-first search (DFS) or breadth-first search (BFS) starting from each friend and counting the number of friends reachable within a distance of 2 (including the starting friend).
Algorithmic Steps:
- Construct a graph where each node represents a friend and each edge represents a friendship.
- Initialize a variable
strongFriendshipsto count the number of strong friendships. - For each friend in the graph, perform a BFS or DFS to traverse the graph up to a distance of 2.
- During the traversal, count the number of friends reached within a distance of 2 (including the starting friend).
- Update
strongFriendshipswith the count of friends reached within a distance of 2. - Repeat steps 3-5 for all friends in the graph.
- Finally, return the total count of strong friendships.
Time Complexity:
- The time complexity of this approach is O(V + E), where V is the number of vertices (friends) and E is the number of edges (friendships) in the graph.
Space Complexity:
- The space complexity is O(V + E) to store the graph and perform the traversal.
:
Solution Code
class Solution {
public int strongFriendships(int n, int[][] friendships) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i < n; i++) {
graph.add(new ArrayList<>());
}
for (int[] friendship : friendships) {
graph.get(friendship[0]).add(friendship[1]);
graph.get(friendship[1]).add(friendship[0]);
}
int strongFriendships = 0;
for (int i = 0; i < n; i++) {
boolean[] visited = new boolean[n];
Queue<Integer> queue = new LinkedList<>();
queue.offer(i);
visited[i] = true;
int distance = 0;
while (!queue.isEmpty() && distance <= 2) {
int size = queue.size();
for (int j = 0; j < size; j++) {
int curr = queue.poll();
strongFriendships++;
for (int neighbor : graph.get(curr)) {
if (!visited[neighbor]) {
queue.offer(neighbor);
visited[neighbor] = true;
}
}
}
distance++;
}
}
return strongFriendships;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1949 (Strong Friendship)?
This page provides optimized solutions for LeetCode problem 1949 (Strong Friendship) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1949 (Strong Friendship)?
The time complexity for LeetCode 1949 (Strong Friendship) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1949 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1949 in Java, C++, or Python.