/**
1736 · Throw garbage
Algorithms
Medium
Accepted Rate
51%
DescriptionSolutionNotesDiscussLeaderboard
Description
There are n garbage bags, and the weight of each garbage bag is between [1.01, 3.00] pounds.
You can take up to 3.00 pounds of garbage one time.
The problem gives the weight of all garbage bags. Please figure out the minimum number of times that you can throw away all the garbage.
0 < n < 1000
Example
Example 1
input:[1.01,2.21,1.30]
output:2
explain:First time throw [1.01,1.30], second time throw[2.21]
Example 2
input:[2.50,2.80]
output:2
explain:First time throw [2.50], second time throw[2.80]
Tags
Company
FactSet
https://blog.csdn.net/qq_46105170/article/details/112131111
https://www.lintcode.com/problem/1736/
*/
import "sort"
/**
* @param BagList: the weight of all garbage bags.
* @return: an integer represent the minimum number of times.
*/
func Count_ThrowTimes (BagList []float32) int {
//
var B []float64
for i := 0; i < len(BagList); i++ {
B = append(B, float64(BagList[i]))
}
sort.Float64s(B)
var res, l, r int = 0, 0, len(B) - 1
var sum float64 = 0
for {
if l >= r {
break
}
sum = B[l] + B[r]
if sum > 3 {
r -= 1
res += 1
} else {
r -= 1
l += 1
res += 1
}
}
if l == r {
res += 1
}
return res
}