华为小朋友拿糖果算法题

    幼儿园小朋友站成一列,按位置1、2、 3... 顺序编号,每个小朋友都拿了若干糖果;请找出3位小朋友,他们拿着相同颜色的糖果,且他们拿的糖果总数不少于其他任何3位小朋友(拿相同颜色糖果)的糖果总数,如果存在多组这样的小朋友,则找出位置编号最小的小朋友所在的组。

设置的前提条件:
1)每个小朋友最少拿1颗糖,最多拿1024颗糖, 且只拿一种颜色的糖果;不存在两个小朋友拿相同颜色相同数目的糖果。
2)糖果颜色只有2种: 1为红色,2为蓝色。
输入描述:
第一行为小朋友的总人数N(N<=1024),后面的N行分别为第1到N小朋友拿到糖果的数量和颜色。
#package huawei92;

import java.util.*;

public class huawei1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] arr = new int[n][2];
for (int i = 0; i < n; i++) {
arr[i][0] = Integer.parseInt(sc.next());
arr[i][1] = Integer.parseInt(sc.next());

    }
    for (Object o : maxCount(arr)) {
        System.out.println(o);
    }
}

public static Object[] maxCount(int[][] arr) {
    TreeMap<Integer, Integer> redMap = new TreeMap<Integer, Integer>();
    TreeMap<Integer, Integer> blueMap = new TreeMap<Integer, Integer>();
    //Num序号
    int Num = 0;
    int redCount = 0;
    int blueCount = 0;
    //key代表数量,value代表序号
    for (int[] arr1 : arr) {
        Num++;
        if (arr1[1] == 1) {
            redMap.put(arr1[0], Num);
            redCount++;
        } else {
            blueMap.put(arr1[0], Num);
            blueCount++;
        }
    }
    int redSum = 0;
    int blueSum = 0;
    //存放序号
    List<Integer> redList = new ArrayList<>(3);
    List<Integer> blueList = new ArrayList<>(3);
    Object[] redKey = redMap.keySet().toArray();
    Object[] blueKey = blueMap.keySet().toArray();

    if (redCount >= 3) {
        for (int i = redKey.length - 1; i > redKey.length - 4; i--) {
            redSum += (Integer) redKey[i];
            redList.add(redMap.get((Integer) redKey[i]));
        }
    }
    if (blueCount >= 3) {
        for (int i = blueKey.length - 1; i > blueKey.length - 4; i--) {
            blueSum += (Integer) blueKey[i];
            blueList.add(blueMap.get((Integer) blueKey[i]));
        }
    }
    Collections.reverse(redList);
    Collections.reverse(blueList);
    Object[] redxuhao = redList.toArray();
    Object[] bluexuhao = blueList.toArray();
    if (redCount < 3 && blueCount < 3) {
        return null;
    } else if (redCount >= 3 && blueCount < 3) {
        for (int i = 0; i < redList.size(); i++) {
            System.out.print(redxuhao[i] + " ");
        }
        System.out.println();
        return new Object[]{1, redSum};
    } else if (blueCount >= 3 && redCount < 3) {
        for (int i = 0; i < blueList.size(); i++) {
            System.out.print(bluexuhao[i] + " ");
        }
        System.out.println();
        return new Object[]{2, blueSum};
    }  else if (blueSum < redSum) {
            for (int i = 0; i < redList.size(); i++) {
                System.out.print(redxuhao[i] + " ");
            }
            System.out.println();
            return new Object[]{1, redSum};

        }else if (blueSum > redSum) {
        for (int i = 0; i < blueList.size(); i++) {
            System.out.print(bluexuhao[i] + " ");
        }
        System.out.println();
        return new Object[]{1, blueSum};

    }
    else {
            int redhe = 0;
            int bluehe = 0;
            for (int i = 0; i < bluexuhao.length; i++) {
                bluehe += (Integer) bluexuhao[i];
            }
            for (int i = 0; i < redxuhao.length; i++) {
                redhe += (Integer) redxuhao[i];
            }
            if (bluehe > redhe) {
                for (int i = 0; i < blueList.size(); i++) {
                    System.out.print(bluexuhao[i] + " ");
                }
                System.out.println();
                return new Object[]{2, blueSum};

            } else {
                for (int i = 0; i < redList.size(); i++) {
                    System.out.print(redxuhao[i] + " ");
                }
                System.out.println();
                System.out.println("1");
                return new Object[]{1, redSum};

            }
        }
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值