DEV Community

Cover image for LeetCode — 2798. Number of Employees Who Met the Target
Ben Pereira
Ben Pereira

Posted on

LeetCode — 2798. Number of Employees Who Met the Target

It’s an easy problem with description being:

There are n employees in a company, numbered from 0 to n - 1. Each employee i has worked for hours[i] hours in the company.

The company requires each employee to work for at least target hours.

You are given a 0-indexed array of non-negative integers hours of length n and a non-negative integer target.

Return the integer denoting the number of employees who worked at least target hours.

This problem is simple and straight forward, you basicaly iterate the array of hours, the ones that are equal or bigger than the target you add into a sum, and then return the sum:

class Solution {
    public int numberOfEmployeesWhoMetTarget(int[] hours, int target) {
        int employeesWhoMetTarget = 0;

        for(int i=0;i<hours.length;i++){
            if(hours[i] >= target) {
                employeesWhoMetTarget++;
            }
        }

        return employeesWhoMetTarget;
    }
}
Enter fullscreen mode Exit fullscreen mode

Runtime: 0ms, faster than 100.00% of Java online submissions.

Memory Usage: 43.08 MB, less than 9.90% of Java online submissions.

if you want to do a bit more fancy you can do like:

class Solution {
    public int numberOfEmployeesWhoMetTarget(int[] hours, int target) {
       return (int) Arrays.stream(hours)
                         .filter(employeeHours -> employeeHours >= target)
                         .count();
    }
}
Enter fullscreen mode Exit fullscreen mode

Runtime: 5ms, faster than 1.24% of Java online submissions.

Memory Usage: 42.53 MB, less than 35.86% of Java online submissions.

Looks nice, but not as good as the first one, if you need performance go for the one that doesn’t use stream.


That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.

Until next post! :)

Top comments (0)