Similar Problems
Similar Problems not available
Find Total Time Spent By Each Employee - Leetcode Solution
Companies:
LeetCode: Find Total Time Spent By Each Employee Leetcode Solution
Difficulty: Easy
Topics: database
Problem:
You are given a list of string logs where each element represents a single employee's log. Each log contains three different pieces of information:
- The employee ID.
- The time the employee started working.
- The time the employee finished working.
Your task is to calculate the total time each employee spent working. Return an array where each element represents an employee ID and the corresponding value represents the total time that employee spent working in seconds. If an employee never started or finished working, their value should be 0.
Example:
Input: logs = ["0:start:0", "1:start:2", "1:end:5", "0:end:6"] Output: [6, 3] Explanation: Employee 0 worked from time 0 to time 6 (6 - 0 = 6 seconds). Employee 1 worked from time 2 to time 5 (5 - 2 = 3 seconds).
Solution:
To solve this problem, we need to keep track of the start time and end time for each employee. We can use a dictionary to store this information.
First, we will iterate through the logs and extract the relevant information for each employee, namely their ID, start time, and end time. We will convert the start and end times to integers representing the number of seconds since the start of the day (i.e. midnight).
Next, we will use the dictionary to keep track of the total time worked by each employee. We will iterate through the logs again, and for each log, we will update the corresponding employee's total time worked by subtracting their start time from their end time and adding it to their current total time worked.
Finally, we will convert the total time worked for each employee from seconds to minutes and return the result as an array.
Code:
Here's the Python code for the solution:
def findTotalTime(logs): time_dict = {} # dictionary to store start and end times for each employee for log in logs: emp_id, action, time = log.split(':') time = int(time) if emp_id not in time_dict: time_dict[emp_id] = [] # create an empty list for this employee's times if action == 'start': time_dict[emp_id].append(time) # add the start time to the list else: time_dict[emp_id].append(time) # add the end time to the list
result = [] # list to store the total time worked by each employee
for emp_id, times in time_dict.items():
total_time = 0
for i in range(0, len(times), 2):
start_time = times[i]
if i + 1 < len(times):
end_time = times[i+1]
total_time += end_time - start_time
result.append(total_time // 60) # convert time from seconds to minutes
return result
Explanation:
We first create an empty dictionary time_dict
to store start and end times for each employee. We then loop through the input logs list and parse out the relevant information for each employee, namely their ID, start time and end time. We then add these times to the corresponding employee's list in the time_dict
dictionary.
Next, we loop through the time_dict
dictionary and calculate the total time worked by each employee. We extract the list of times for each employee and loop through it. For each pair of start and end times in the list, we calculate the difference (in seconds) between the end and start times and add this to the total time worked by the employee.
Finally, we convert the total time worked for each employee from seconds to minutes and store them in the result
list. We then return this list as the solution of the problem.
Find Total Time Spent By Each Employee Solution Code
1