From 33a155129daa82c14236733b71090107a6fa50ef Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Thu, 22 Jan 2026 10:25:44 +0800 Subject: [PATCH] Add unit tests for the "Number of Employees Who Met the Target" problem. --- src/NumberOfEmployeesWhoMetTheTarget2798.java | 17 --------- .../NumberOfEmployeesWhoMetTheTarget2798.java | 23 ++++++++++++ ...berOfEmployeesWhoMetTheTarget2798Test.java | 35 +++++++++++++++++++ 3 files changed, 58 insertions(+), 17 deletions(-) delete mode 100644 src/NumberOfEmployeesWhoMetTheTarget2798.java create mode 100644 src/main/java/com/leetcode/easy/NumberOfEmployeesWhoMetTheTarget2798.java create mode 100644 src/test/java/com/leetcode/easy/NumberOfEmployeesWhoMetTheTarget2798Test.java diff --git a/src/NumberOfEmployeesWhoMetTheTarget2798.java b/src/NumberOfEmployeesWhoMetTheTarget2798.java deleted file mode 100644 index aeebfb0..0000000 --- a/src/NumberOfEmployeesWhoMetTheTarget2798.java +++ /dev/null @@ -1,17 +0,0 @@ -public class NumberOfEmployeesWhoMetTheTarget2798 { - public int numberOfEmployeesWhoMetTarget(int[] hours, int target) { - int res = 0; - for (int hour : hours) { - if (hour >= target) { - res++; - } - } - return res; - } - - public static void main(String[] args) { - int[] hours = {0, 1, 2, 3, 4}; - int target = 2; - assert 3 == new NumberOfEmployeesWhoMetTheTarget2798().numberOfEmployeesWhoMetTarget(hours, target); - } -} diff --git a/src/main/java/com/leetcode/easy/NumberOfEmployeesWhoMetTheTarget2798.java b/src/main/java/com/leetcode/easy/NumberOfEmployeesWhoMetTheTarget2798.java new file mode 100644 index 0000000..c56f4f8 --- /dev/null +++ b/src/main/java/com/leetcode/easy/NumberOfEmployeesWhoMetTheTarget2798.java @@ -0,0 +1,23 @@ +// Tags: Array +package com.leetcode.easy; + +public class NumberOfEmployeesWhoMetTheTarget2798 { + /** + * Time Complexity: O(n) where n is the length of the hours array + * - We iterate through the entire array once + * - Each element is checked exactly once + *

+ * Space Complexity: O(1) + * - Only uses a constant amount of extra space (variables res and hour) + * - No additional data structures are allocated + */ + public int numberOfEmployeesWhoMetTarget(int[] hours, int target) { + int res = 0; + for (int hour : hours) { + if (hour >= target) { + res++; + } + } + return res; + } +} diff --git a/src/test/java/com/leetcode/easy/NumberOfEmployeesWhoMetTheTarget2798Test.java b/src/test/java/com/leetcode/easy/NumberOfEmployeesWhoMetTheTarget2798Test.java new file mode 100644 index 0000000..674c9f2 --- /dev/null +++ b/src/test/java/com/leetcode/easy/NumberOfEmployeesWhoMetTheTarget2798Test.java @@ -0,0 +1,35 @@ +package com.leetcode.easy; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class NumberOfEmployeesWhoMetTheTarget2798Test { + private NumberOfEmployeesWhoMetTheTarget2798 solution; + + @BeforeEach + void setUp() { + solution = new NumberOfEmployeesWhoMetTheTarget2798(); + } + + @Test + void testNumberOfEmployeesWhoMetTarget_Example1() { + int[] hours = {0, 1, 2, 3, 4}; + int target = 2; + + int result = solution.numberOfEmployeesWhoMetTarget(hours, target); + + assertEquals(3, result); + } + + @Test + void testNumberOfEmployeesWhoMetTarget_Example2() { + int[] hours = {5, 1, 4, 2, 3}; + int target = 6; + + int result = solution.numberOfEmployeesWhoMetTarget(hours, target); + + assertEquals(0, result); + } +}