Lintcode1736 · Throw garbage go

/**
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                                                                              
}                                                                                           
                                                                                            
                                                                                            
                                                                                            
                                                                                            
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值