diff --git a/internal/Leetcode/1701/1701.go b/internal/Leetcode/1701/1701.go new file mode 100644 index 0000000..62bda8d --- /dev/null +++ b/internal/Leetcode/1701/1701.go @@ -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)) +} diff --git a/internal/Leetcode/1701/1701_test.go b/internal/Leetcode/1701/1701_test.go new file mode 100644 index 0000000..2a97f42 --- /dev/null +++ b/internal/Leetcode/1701/1701_test.go @@ -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) + } + }) + } +}