Skip to content

Commit

Permalink
feat(1701): Medium
Browse files Browse the repository at this point in the history
There is a restaurant with a single chef. You are given an array customers, where customers[i] = [arrivali, timei]:

arrivali is the arrival time of the ith customer. The arrival times are sorted in non-decreasing order.
timei is the time needed to prepare the order of the ith customer.
When a customer arrives, he gives the chef his order, and the chef starts preparing it once he is idle. The customer waits till the chef finishes preparing his order. The chef does not prepare food for more than one customer at a time. The chef prepares food for customers in the order they were given in the input.

Return the average waiting time of all customers. Solutions within 10-5 from the actual answer are considered accepted.
  • Loading branch information
DziedzicGrzegorz committed Jul 9, 2024
1 parent 46da107 commit 17064be
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/Leetcode/1701/1701.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package _1701

func averageWaitingTime(customers [][]int) float64 {
waitingTime, timeLine := 0, 0
for _, customer := range customers {
if timeLine < customer[0] {
timeLine = customer[0]
}
waitingTime += customer[1] + (timeLine - customer[0])
timeLine += customer[1]
}
return float64(waitingTime) / float64(len(customers))
}
26 changes: 26 additions & 0 deletions internal/Leetcode/1701/1701_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package _1701

import (
"testing"
)

func Test_averageWaitingTime(t *testing.T) {
type args struct {
customers [][]int
}
tests := []struct {
name string
args args
want float64
}{
{name: "Case 2", args: struct{ customers [][]int }{customers: [][]int{{5, 2}, {5, 4}, {10, 3}, {20, 1}}}, want: 3.25000},
{name: "Case 1", args: struct{ customers [][]int }{customers: [][]int{{1, 2}, {2, 5}, {4, 3}}}, want: 5.00000},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := averageWaitingTime(tt.args.customers); got != tt.want {
t.Errorf("averageWaitingTime() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 17064be

Please sign in to comment.