-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDailyCodingProblem732.go
60 lines (46 loc) · 1.15 KB
/
DailyCodingProblem732.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import "fmt"
/*Good morning! Here's your coding interview problem for today.
This problem was asked by Glassdoor.
An imminent hurricane threatens the coastal town of Codeville.
If at most two people can fit in a rescue boat, and the maximum weight
limit for a given boat is k, determine how many boats will be needed to save everyone.
For example, given a population with weights [100, 200, 150, 80] and a
boat limit of 200, the smallest number of boats required will be three.*/
func main() {
// Size of list
var size int = 0
fmt.Print("Enter size of list : ")
fmt.Scan(&size)
slc := make([]int, size)
// Enter value into a slice
for i := 0; i < size; i++ {
num := 0
fmt.Print("Enter a number : ")
fmt.Scan(&num)
slc[i] = num
}
// Weight
var weight int = 0
fmt.Print("Enter weight : ")
fmt.Scan(&weight)
fmt.Println(getBoats(slc, weight))
}
func getBoats(num []int, weight int) int {
sum := 0
notEven := false
for i := range num {
sum += num[i]
//fmt.Println(sum)
}
boats := sum / weight
if sum%weight > 0 {
notEven = true
} else {
notEven = false
}
if notEven == true {
return boats + 1
}
return boats
}