【华为OD题库-024】组装最大可靠性设备-java

题目

一个设备由N种类型元器件组成(每种类型元器件只需要一个, 类型type编号从0~N-1);每个元器件均有可靠性属性reliability,可靠性越高的器件其价格price越贵。而设备的可靠性由组成设备的所有器件中可靠性最低的器件决定。给定预算S,购买N种元器件(每种类型元器件都需要购买一个), 在不超过预算的情况下,请给出能够组成的设备的最大可靠性。
输入描述

S N // S总的预算,N元器件的种类

total //元器件的总数,每种型号的元器件可以有多种:

此后有total行具体器件的数据

type reliability price //type整数类型,代表元器件的类型编号从0 ~ N-1; reliabily整数类型,代表元器件的可靠性: price 整数类型,代表元器件的价格。
输出描述

符合预算的设备的最大可靠性,如果预算无法买产N种器件,则返回-1
备注

0 <= S,price <= 10000000

0<=N<= 100

0 <=type <= N-1

0 <= total <= 100000

0 < reliability <= 100000
示例1:
输入

500 3

0 80 100

0 90 200

1 50 50

1 70 210

2 50 100

2 60 150
输出

60
说明

预算500,设备需要3种元件组成,方案类型0的第一个(可靠性80),

类型1的第二个可靠性70).

类型2的第二个(可靠性60).

可以使设备的可靠性最大60
示例2:
输入

100 1

0 90 200
输出

-1

说明

组成设备需要1个元件,但是元件价格大于预算,因此无法组成设备,返回-1

思路

组合题,从total个元器件中选取N个,使其价格不超过S,求满足条件的组合中可靠性的最大值(可靠性由组合中最低的元器件可靠性决定)

这类组合问题的大概思路可以参考另一篇博文:【JAVA-排列组合】一个套路速解排列组合题

针对本题来说,注意以下几点:

  1. 不再是对简单的int进行选取,而是一个java对象,新建Device对象,含有type,price,reliability三个属性
  2. 同一个类型不能重复选择,要选取N个元器件,测试数据也只会给出N种元器件(每种型号的元器件可以有多种),且其type编号是0~N-1。所以还是可以使用一个int数组标识某种元器件是否已被使用。int\[\] used = new intN; 被使用时标记:useddevices\[i.getType()] = 1;此时应该剪枝。
  3. 可靠性计算方法为path中可靠性最低的元器件,所以每次进行计算时,可靠性的表达式应该为:Math.min(reliability, devicesi.getReliability())
  4. 题目只需要求最终最大的可靠性,不需要输出具体的组合,所以不需要在外层设置变量res存放各种组合的结果。只需要判断,当path长度等于N,并且价格不超过预算时,其可靠性是否变得更大,最终输出满足条件的最大的可靠性即可

题解

java 复制代码
package hwod;

import java.util.*;

public class LargestReliability {
    private static int ans = -1;//最终结果

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int totalPrice = sc.nextInt(); //预算
        int targetNum = sc.nextInt();//需要的元器件种类
        int total = sc.nextInt(); //总共的元器件数量
        sc.nextLine();
        Device[] devices = new Device[total];

        for (int i = 0; i < total; i++) {
            String[] lines = sc.nextLine().split(" ");
            Device device = new Device(Integer.parseInt(lines[0]), Integer.parseInt(lines[2]), Integer.parseInt(lines[1]));
            devices[i] = device;
        }
        //先排序,根据种类、价格升序,可靠性降序排序
        //非必须,不排序也不影响
        Arrays.sort(devices, (o1, o2) -> {
            if (o1.getType() != o2.getType()) return o1.getType() - o2.getType();
            if (o1.getPrice() != o2.getPrice()) return o1.getPrice() - o2.getPrice();
            return o2.getReliability() - o1.getReliability();
        });
        System.out.println(largestReliability(devices, totalPrice, targetNum));
    }

    //从devices中选择targetNum种元器件,价格不超过totalPrice,能够得到的最高可靠性是多少?
    private static int largestReliability(Device[] device, int totalPrice, int targetNum) {
        //从total里面选取target
        LinkedList<Device> path = new LinkedList<>();//组合问题,临时选到path中去
        int[] used = new int[targetNum];
        dfs(device, 0, path, used, totalPrice, targetNum, Integer.MAX_VALUE);
        return ans;
    }

    private static void dfs(Device[] devices, int begin, LinkedList<Device> path, int[] used, int totalPrice, int targetNum, int reliability) {
        if (path.size() == targetNum) {
            if (totalPrice >= 0 && reliability > ans) {
                ans = reliability;
            }
            return;
        }
        for (int i = begin; i < devices.length; i++) {
            if (used[devices[i].getType()] == 1) continue;//剪枝,不能选择同类型的type
            path.addLast(devices[i]);
            used[devices[i].getType()] = 1;
            dfs(devices, i + 1, path, used, totalPrice - devices[i].getPrice(), targetNum, Math.min(reliability, devices[i].getReliability()));
            path.removeLast();
            used[devices[i].getType()] = 0;

        }
    }
}

class Device {
    private int type;
    private int price;
    private int reliability;

    public Device(int type, int price, int reliability) {
        this.type = type;
        this.price = price;
        this.reliability = reliability;
    }

    public int getType() {
        return type;
    }

    public int getPrice() {
        return price;
    }

    public int getReliability() {
        return reliability;
    }
}

推荐

如果你对本系列的其他题目感兴趣,可以参考华为OD机试真题及题解(JAVA),查看当前专栏更新的所有题目。

相关推荐
小蒜学长8 小时前
基于SpringBoot和Vue的低卡食品销售系统的设计与实现(代码+数据库+LW)
java·后端·springboot·健康管理·低卡食品销售系统
CRMEB系统商城9 小时前
前后端技术栈全面换代!CRMEB 多商户(Java)v3.0更新预告
java·开发语言·小程序·php
Sayai9 小时前
Neo4j 内嵌模式(Embedded)实战:Java 嵌入式 vs 服务端部署的写入性能对比与 GC 调优
java·开发语言·性能优化·neo4j·图数据库
木头科技9 小时前
AI 工程化第四篇】Spring AI Agent 线上可观测实战:Token 成本、调用链、工具耗时、RAG 命中率怎么监控
java·人工智能·spring
m0_734172429 小时前
Python列表切片为什么不改变原列表
java
kiss strong9 小时前
idea显示前进后退按钮
java·ide·intellij-idea
caoerzhong10 小时前
跨境海外仓怎么管:JeeWMS 开源 Java 仓库管理系统打通头程、海外仓与尾程
java·开发语言·开源
MayBaymax10 小时前
Elasticsearch 原理与用法
java·elasticsearch
马剑威(威哥爱编程)10 小时前
【AI全栈后端12-02】Spring Boot 跑通第一个 AI 对话接口:HR 政策问答机器人实战
java·人工智能·spring boot·机器人
IT枫斗者枫哥10 小时前
AI返回合法JSON,字段就可信吗?给抽取结果补一道业务校验
java·人工智能·后端