Lintcode1851 · Buy Passes go

/**
1851 · Buy Passes
Algorithms
Medium
Accepted Rate
52%

DescriptionSolutionNotesDiscussLeaderboard
Description
Alex plans on visiting the museum and is at the counter to purchase passes for the same. The administrators have decided not to sell group passes, and are providing only one pass at a time. If a visitor needs more than one pass, he/she has to pass through the queue again to reach the counter and buy the next pass, Alex wants to buy many passe.Considering that the visitors and number of passes each visitor needs is known, how much time does Alex require to buy all passes?Alex’s place in the queue to the counter will be given, and each transaction takes 1 unit of time. The time needed to go to the back of the line each time can be ignored.

|arr|<=100000
arr[i]<=10000

Example
Example 1:

Input:
arr=[1,2,5],k=1
Output:
4
Explanation:
There is three persons.0,1,2.Alex is 1.
at 1 time,0(1)<-1(2)<-2(5).people 0 gets one pass.
at 2 time,1(2)<-2(5) Alex gets one pass,and he goes to the back of the line.
at 3 time,2(5)<-1(1),people 2 gets one pass,and he goes to the back of the line.
at 4 time,1(1)<-2(4),Alex gets one pass. Alex buys all passes.
Example 2:

Input:
arr=[3,2,1], k = 0,
Output:
6
Tags

https://blog.csdn.net/qq_46105170/article/details/104872690

https://www.lintcode.com/problem/1851/
*/

/**
 * @param arr: the line
 * @param k: Alex place
 * @return: the time when Alex requires to buy all passes
 */
func buyPasses (arr []int, k int) int {
	// Write your code here.
	if arr == nil || len(arr) == 0 {
		return 0
	}
	var time int = 0
	for i := 0; i < len(arr); i++ {
		if i <= k {
			time += Min(arr[i], arr[k])
		} else {
			time += Min(arr[i], arr[k] - 1)
		}
	}
	return time
}

func Min(a, b int) int {
	if a < b {
		return a
	}
	return b
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值