Similar Problems

Similar Problems not available

Sales Person - Leetcode Solution

Companies:

LeetCode:  Sales Person Leetcode Solution

Difficulty: Easy

Topics: database  

The Sales Person problem on Leetcode is a coding problem that requires you to write a function that takes a list of sales and returns the name of the salesperson who has sold the most.

The problem can be solved in several ways, but one possible solution is described below:

  1. Create a dictionary to store the sales of each salesperson, with the salesperson's name as the key and the total sales as the value.
  2. Iterate over the list of sales and update the dictionary with each sale. If the salesperson does not exist in the dictionary, add them with the sales amount as their first sale.
  3. Find the salesperson with the highest total sales in the dictionary using the max() function.
  4. Return the name of the salesperson with the highest sales.

Here's a sample implementation of the function:

def best_seller(sales):
    sales_dict = {}
    for s in sales:
        name, amount = s.split(':')
        amount = int(amount)
        if name in sales_dict:
            sales_dict[name] += amount
        else:
            sales_dict[name] = amount
    return max(sales_dict, key=sales_dict.get)

In this implementation, we split each sale string into the salesperson's name and the sales amount using the ":" delimiter. We then convert the sales amount to an integer and update the sales_dict.

Finally, we use the max() function to find the salesperson with the highest sales in the dictionary and return their name.

Here's an example use case:

sales = ["John:500", "Sarah:1000", "John:700", "Tom:1200", "Sarah:900", "Tom:800"]
best_seller(sales)

This would return "Sarah" as the best selling salesperson, since she has sold the most with a total of 1900.

Sales Person Solution Code

1