拼多多笔试第三题

哪位看看俺这代码哪里有问题?

package com.rockTechnology;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Test4 {
    private static int minHot(int[] hot, int[] taste, int t){
        if (t == 0){
            return 0;
        }

        ArrayList<Integer> hotList = backTrack(hot, taste, t, 0, 0, 0, new ArrayList<Integer>(), 2);
        if (hotList.size() == 0){
            return -1;
        }
        Collections.sort(hotList, new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });
        return hotList.get(0);
    }

    private static ArrayList<Integer> backTrack(int[] hot, int[] taste, int t, int index, int tasteSum, int hotSum, ArrayList<Integer> hotList, int times) {
        if (tasteSum >= t){
            hotList.add(hotSum);
            return hotList;
        }
        if (index >= hot.length){
            return hotList;
        }
        if (times > 0){
            backTrack(hot, taste, t, index + 1, tasteSum + taste[index], hotSum + hot[index], hotList, times - 1);
        }
        backTrack(hot, taste, t, index + 1, tasteSum, hotSum, hotList, times);
        return hotList;
    }


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt() + in.nextInt();
        int[] hot = new int[n];
        int[] taste = new int[n];
        int t = in.nextInt();
        for (int i = 0; i < n; i++) {
            hot[i] = in.nextInt();
            taste[i] = in.nextInt();
        }
        System.out.println(minHot(hot, taste, t));
    }
}



#笔试题目#
全部评论
超时了吧你这
点赞 回复 分享
发布于 2020-08-02 21:09

相关推荐

2025年10月3日中午,在写完定时一年后发给自己的信之后,敲下键盘,写下这篇文字。我把标题的“所有人”加了引号,因为如我们所见,确实有的人顺风顺水,每天过的很开心,或是早早进入大厂,或是年纪轻轻就拿到了高薪offer,或是过着可能我努力十年也不一定实现的生活。但也许,不是每个人的痛苦都能被别人看到的,这个月我经常会哭,被骗6000块钱、手上钱不够导致拖欠房租、生活还要借朋友钱、国庆长假也没有钱去旅游,互联网公司不稳定担心试用期不过(毕竟上段实习就是被裁了,一有点风吹草动就害怕),但这样的我,不是所有人都知道的,居然是有些朋友的羡慕对象。回忆我的七年“长跑”别人都是多年幸福的恋爱长跑,我没有恋...
故事和酒66:让每一颗种子找到合适自己的生长方式,最终绽放出独一无二的花朵,这远比所有人都被迫长成同一棵“参天大树”的世界,更加美好和富有生机。这是社会和环境的问题,而不是我们的问题。然而就是在这样的环境中,楼主依然能突破自我,逆势成长,其中的艰辛可想而知。这一路的苦难终究会化作你成长的养料
你小时候最想从事什么职业
点赞 评论 收藏
分享
牛客小菜鸡66:boss里面,招人的叫老板,找工作的叫牛人
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务