LeetCode 2372: Calculate the Influence of Each Salesperson Solution

Master LeetCode problem 2372 (Calculate the Influence of Each Salesperson), 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.

2372. Calculate the Influence of Each Salesperson

Medium

Problem Explanation

Explanation:

To calculate the influence of each salesperson, we can model the relationships between salespeople as a directed graph where each edge represents influence from one salesperson to another. We can then perform a topological sort on this graph to determine the influence of each salesperson.

Algorithm:

  1. Create a directed graph where each vertex represents a salesperson and each edge represents influence from one salesperson to another.
  2. Perform a topological sort on the graph to determine the order in which salespeople influence each other.
  3. For each salesperson in the sorted order, calculate their influence by summing the influences of the salespeople who influence them.
  4. Return the influence of each salesperson.

Time Complexity:

  • Building the graph: O(N), where N is the number of relationships between salespeople.
  • Topological sort: O(V + E), where V is the number of vertices (salespeople) and E is the number of edges (relationships).
  • Calculating influence: O(V), where V is the number of vertices. Overall time complexity: O(N + V + E)

Space Complexity:

  • Graph: O(V + E)
  • Influence array: O(V) Overall space complexity: O(V + E)

: :

Solution Code

import java.util.*;

class Solution {
    public int[] calculateInfluence(int n, int[][] sales) {
        List<Integer>[] graph = new List[n];
        int[] influence = new int[n];

        for (int i = 0; i < n; i++) {
            graph[i] = new ArrayList<>();
        }

        for (int[] sale : sales) {
            graph[sale[0]].add(sale[1]);
        }

        for (int i = 0; i < n; i++) {
            for (int j : graph[i]) {
                influence[j] += 1 + influence[i];
            }
        }

        return influence;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2372 (Calculate the Influence of Each Salesperson)?

This page provides optimized solutions for LeetCode problem 2372 (Calculate the Influence of Each Salesperson) 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 2372 (Calculate the Influence of Each Salesperson)?

The time complexity for LeetCode 2372 (Calculate the Influence of Each Salesperson) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2372 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2372 in Java, C++, or Python.

Back to LeetCode Solutions