algoadvance

Leetcode 2798. Number of Employees Who Met the Target

Problem Statement

Number of Employees Who Met the Target

You are given a list of integers representing the number of units sold by each employee during a certain period. Also, you are given an integer target which represents the minimum number of units that an employee needs to sell to be considered as meeting the target.

Write a function that returns the number of employees who met or exceeded the target sales.

Function Signature

int numberOfEmployeesWhoMetTarget(vector<int>& sales, int target);

Clarifying Questions

  1. Input Bounds & Constraints:
    • What is the range of the number of employees (length of the sales vector)?
    • What is the range of the sales units and the target value?
  2. Edge Cases:
    • What should be returned if there are no employees (i.e., the list is empty)?
    • Should we consider non-positive target values, and how to handle them?

Strategy

  1. Iterate through the list of sales:
    • Initialize a counter to zero.
    • For each employee’s sales value:
      • Compare it with the target.
      • If it meets or exceeds the target, increment the counter.
  2. Return the counter at the end.

This approach ensures we check each employee’s sales exactly once, making the solution efficient.

Code

#include <vector>

int numberOfEmployeesWhoMetTarget(std::vector<int>& sales, int target) {
    int count = 0;
    for (int sale : sales) {
        if (sale >= target) {
            count++;
        }
    }
    return count;
}

Time Complexity

This solution is efficient and straightforward for the given problem constraints.

Cut your prep time in half and DOMINATE your interview with AlgoAdvance AI