Hello, I started doing leetcode to expand my programming understanding. I did the problem which employee earns more than their manager, and want to know if I calculated the efficiency correctly. / https://leetcode.com/problems/employees-earning-more-than-their-managers/
I put the data into two separate tables to access them more efficiently.
local employees = {
[1] = {
Name = "Joe",
Salary = 70000,
ManagerId = 1
},
[2] = {
Name = "Henry",
Salary = 80000,
ManagerId = 2
}
}
local managers = {
[1] = {
Name = "Sam",
Salary = 60000,
},
[2] = {
Name = "Max",
Salary = 90000
}
}
For checking all the employees Salary I use a for loop. This makes it O(n) n being the length of the dictionary and since each check of salary is O(1) it will just be O(n) for whole loop. The if statement is a logical calculation and I read that those are O(1). This is the most efficient way because I go through a hashtable and then to a look up (I think lol).
local employees = {
[1] = {
Name = "Joe",
Salary = 70000,
ManagerId = 1
},
[2] = {
Name = "Henry",
Salary = 80000,
ManagerId = 2
}
}
local managers = {
[1] = {
Name = "Sam",
Salary = 60000,
},
[2] = {
Name = "Max",
Salary = 90000
}
}
for _, employeeData in pairs(employees) do
if employeeData.Salary > managers[employeeData.ManagerId].Salary then
print(employeeData.Name .. " salary of " .. employeeData.Salary .. " is more than their manager, " .. managers[employeeData.ManagerId].Name .. " salary of" .. managers[employeeData.ManagerId].Salary)
end
end