最大口罩产量

题目描述

已知莫工厂未来一段时间,每天口罩的生产产能一次记录于数组productions,每天的生产计划依次记录于数组plans,plans[i]值为1表示生产,值为0表示休息由于疫情影响,口罩需求增加,即:在这window天中原计划休息的,开工生产在这window天之外的,生产计划不变请制定合适的港公方案,使得这段时间的总产量最大,并返回该最大值

输入

第一个参数为整数序列productions,800<=productions<=1000
第二个参数为行为整数序列plans,值仅为0或1
第三个参数为window
注:5<=productions.length==plans.length<=100000;1<=window<=productions.length

输出

一个整数,表示最大口罩生产量

样例1

输入

 803,804,805,802,803,804,805,804
 1,0,1,0,1,0,1,0
 3

输出

4824

样例2

输入

980,910,1000,940,973
 1,1,1,1,1
 5

输出

4803

Java算法源码

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class MaxProduceGauze {

    public static int getMaxQuantity(int[] productions, int[] plans, int window) {
        // 最大产量
        int maxQuantity;
        // 轮训window开始位置
        int start;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < productions.length; i++) {
            start = i;
            // 每次轮训产量和
            int sum = 0;
            // 初始化轮训窗口window索引
            int index = 0;
            for (int j = 0; j < productions.length; j++) {
                if (j < start && (plans[j] == 1)) {
                    sum += productions[j];
                }
                if (j >= start) {
                    if (index < window) {
                        sum += productions[j];
                        index++;
                    } else {
                        if (plans[j] == 1) {
                            sum += productions[j];
                        }
                    }
                }
            }
            list.add(sum);
        }
        maxQuantity = Collections.max(list);
        return maxQuantity;
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in, StandardCharsets.UTF_8.name());
        while (cin.hasNext()) {
            String[] productionsStr = cin.nextLine().trim().split(",");
            String[] planStr = cin.nextLine().trim().split(",");
            int[] productions = new int[productionsStr.length];
            int[] plans = new int[planStr.length];
            int window = cin.nextInt();
            for (int i = 0; i < productionsStr.length; i++) {
                productions[i] = Integer.parseInt(productionsStr[i]);
            }
            for (int i = 0; i < planStr.length; i++) {
                plans[i] = Integer.parseInt(planStr[i]);
            }
            System.out.println(getMaxQuantity(productions, plans, window));
        }
    }
}
  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软软的铲屎官

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值