HackerRank之Climbing the Leaderboard题解

题目
在这里插入图片描述
具体题目地址
Climbing the Leaderboard

思路

好不容易通过了所有测试用例,使用单纯的暴力破解只能通过一部分用例,部分用例会超时
思路,使用list列表存储所有不重复的数字,因为本身数组就排序了,所以不用排序,然后因为alice数组也是从小到大排列,所以名次肯定也是从后向前排,所以从list后面开始计算位置,并且每查找一个,就把list相应最后的一个元素删除,减少下一个查找的工作量,防止超时,还要及时保存最后被删除的元素,防止list的数量为0时无法比较。

代码

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the climbingLeaderboard function below.
    static int[] climbingLeaderboard(int[] scores, int[] alice) {
       List<Integer> list=new ArrayList<>();
        int[] res=new int[alice.length];
        for(int i=0;i<scores.length;i++){
            if(i>0){
                if(scores[i]!=list.get(list.size()-1)){
                    list.add(scores[i]);
                }
            }else{
                list.add(scores[i]);
            }
        }
        
        int temp=list.get(list.size()-1);
        for(int i=0;i<alice.length;i++){
           while(list.size()>0&&alice[i]>list.get(list.size()-1)){
               temp=list.get(list.size()-1);
               list.remove(list.size()-1);
           }
           if(list.size()==0){
               if(temp<alice[i]){
                    res[i]=1;
               }else{
                   res[i]=2;
               }
           }else if(alice[i]==list.get(list.size()-1)){
               res[i]=list.size();
           }else{
               res[i]=list.size()+1;
           }
        }

        return res;
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int scoresCount = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        int[] scores = new int[scoresCount];

        String[] scoresItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < scoresCount; i++) {
            int scoresItem = Integer.parseInt(scoresItems[i]);
            scores[i] = scoresItem;
        }

        int aliceCount = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        int[] alice = new int[aliceCount];

        String[] aliceItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < aliceCount; i++) {
            int aliceItem = Integer.parseInt(aliceItems[i]);
            alice[i] = aliceItem;
        }

        int[] result = climbingLeaderboard(scores, alice);

        for (int i = 0; i < result.length; i++) {
            bufferedWriter.write(String.valueOf(result[i]));

            if (i != result.length - 1) {
                bufferedWriter.write("\n");
            }
        }

        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值