Skip to content

Commit f4b45ca

Browse files
committed
[20Aug] Maximum units on truck func
1 parent 99d25af commit f4b45ca

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

satyam-sundaram/max-units-on-truck.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import (
2+
"fmt"
3+
"sort"
4+
)
5+
6+
func Min(a, b int) int {
7+
if a < b {
8+
return a
9+
}
10+
return b
11+
}
12+
13+
func maximumUnits(boxTypes [][]int, truckSize int) int {
14+
sort.Slice(boxTypes, func(i, j int) bool {
15+
return boxTypes[i][1] >= boxTypes[j][1]
16+
})
17+
load := 0
18+
totalUnits := 0
19+
for _, box := range boxTypes {
20+
load = Min(box[0], truckSize)
21+
totalUnits += box[1] * load
22+
truckSize -= load
23+
if truckSize == 0 {
24+
break
25+
}
26+
}
27+
return totalUnits
28+
}

0 commit comments

Comments
 (0)