@@ -2,7 +2,6 @@ package main
22
33import (
44 "fmt"
5- "sort"
65 "sync"
76)
87
@@ -20,66 +19,49 @@ type Manager struct {
2019}
2120
2221// AddEmployee adds a new employee to the manager's list.
23- func (m * Manager ) AddEmployee (e Employee ) { // Time: O(n)
22+ func (m * Manager ) AddEmployee (e Employee ) { // O(1) or O(n)
2423 m .mu .Lock ()
2524 defer m .mu .Unlock ()
26- // sort.Slice(m.Employees, func(i, j int) bool {
27- // return m.Employees[i].ID < m.Employees[j].ID
28- // })
29- idx := m .search (e .ID )
30- if idx == - 1 {
31- idx = len (m .Employees )
32- }
33- m .Employees = append (m .Employees , Employee {})
34- copy (m .Employees [idx + 1 :], m .Employees [idx :])
35- m .Employees [idx ] = e
25+ m .Employees = append (m .Employees , e ) // O(1) or O(n)
3626 m .totalSalary += e .Salary
3727}
3828
3929// RemoveEmployee removes an employee by ID from the manager's list.
40- func (m * Manager ) RemoveEmployee (id int ) bool { // Time: O(n)
30+ func (m * Manager ) RemoveEmployee (id int ) bool { // O(n)
4131 m .mu .Lock ()
4232 defer m .mu .Unlock ()
43- index := m .search (id ) // O(log n)
44- if index != - 1 {
45- m .totalSalary -= m .Employees [index ].Salary
46- m .Employees = append (m .Employees [:index ], m .Employees [index + 1 :]... ) // O(n)
47- return true
33+ for index := range m .Employees {
34+ if m .Employees [index ].ID == id {
35+ m .totalSalary -= m .Employees [index ].Salary
36+ m .Employees = append (m .Employees [:index ], m .Employees [index + 1 :]... )
37+ return true
38+ }
4839 }
4940 return false
5041}
5142
5243// GetAverageSalary calculates the average salary of all employees.
53- func (m * Manager ) GetAverageSalary () float64 { // Time: O(1)
44+ func (m * Manager ) GetAverageSalary () float64 { // O(1)
5445 m .mu .RLock ()
5546 defer m .mu .RUnlock ()
56- if n := len (m .Employees ); n > 0 {
57- return m . totalSalary / float64 ( n )
47+ if len (m .Employees ) == 0 {
48+ return 0
5849 }
59- return 0
50+ return m . totalSalary / float64 ( len ( m . Employees ))
6051}
6152
6253// FindEmployeeByID finds and returns an employee by their ID.
63- func (m * Manager ) FindEmployeeByID (id int ) * Employee { // Time: O(log n)
54+ func (m * Manager ) FindEmployeeByID (id int ) * Employee { // O( n)
6455 m .mu .RLock ()
6556 defer m .mu .RUnlock ()
66- index := m .search (id )
67- if index != - 1 {
68- return & m .Employees [index ]
57+ for index := range m .Employees {
58+ if m .Employees [index ].ID == id {
59+ return & m .Employees [index ]
60+ }
6961 }
7062 return nil
7163}
7264
73- func (m * Manager ) search (id int ) int { // Time: O(log n)
74- index := sort .Search (len (m .Employees ), func (i int ) bool {
75- return m .Employees [i ].ID >= id
76- }) // sort.Search returns an index where a new item could be inserted, so we do not receive -1 or an error
77- if index < len (m .Employees ) && m .Employees [index ].ID == id {
78- return index
79- }
80- return - 1
81- }
82-
8365func main () {
8466 manager := Manager {}
8567 manager .AddEmployee (Employee {ID : 1 , Name : "Alice" , Age : 30 , Salary : 70000 })
0 commit comments