Leetcode 2798. 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.
int numberOfEmployeesWhoMetTarget(vector<int>& sales, int target);
This approach ensures we check each employee’s sales exactly once, making the solution efficient.
#include <vector>
int numberOfEmployeesWhoMetTarget(std::vector<int>& sales, int target) {
int count = 0;
for (int sale : sales) {
if (sale >= target) {
count++;
}
}
return count;
}
n
is the number of elements in the sales vector. This is because we are iterating through the list once.This solution is efficient and straightforward for the given problem constraints.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?