很多人排队问题

There is a queue for the self-checkout tills at the supermarket. Your task is write a function to calculate the total time required for all the customers to check out!


The function has two input variables:

  • customers: an array (list in python) of positive integers representing the queue. Each integer represents a customer, and its value is the amount of time they require to check out.
  • n: a positive integer, the number of checkout tills.

The function should return an integer, the total time required.


EDIT: A lot of people have been confused in the comments. To try to prevent any more confusion:

  • There is only ONE queue, and
  • The order of the queue NEVER changes, and
  • Assume that the front person in the queue (i.e. the first element in the array/list) proceeds to a till as soon as it becomes free.
  • The diagram on the wiki page I linked to at the bottom of the description may be useful.

So, for example:

queueTime([5,3,4], 1)
// should return 12
// because when n=1, the total time is just the sum of the times

queueTime([10,2,3,3], 2)
// should return 10
// because here n=2 and the 2nd, 3rd, and 4th people in the 
// queue finish before the 1st person has finished.

queueTime([2,3,10], 2)
// should return 12输入的数据是一个数组但是,n的个数可以是100、或者7都可以,数组排队问题
import java.util.Arrays;
import java.util.stream.IntStream;
public class Solution {

        public static int solveSuperMarketQueue(int[] customers, int n) {

            if((customers.length == 0) || (n == 0)) return 0;
            int sum = IntStream.of(customers).sum();
            if(n == 1) return sum;

            int times[] = new int[n];

            for (int client: customers){
                Arrays.sort(times);
                times[0] += client;
            }
            return Arrays.stream(times).max().getAsInt();
        
    }
}

用一个数组来组装,每次拍完一个队就要排一下序。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值