Java8为什么我的解决方案比简洁的解决方案更快? (hackerrank巧克力盛宴)

时间:2016-01-24 21:11:23

标签: java

编辑:简化我的解决方案。 编辑:删除基于意见的次要问题。

背景:在一两个星期前,使用hackerranks问题作为练习和stackoverflow搜索+谷歌作为我的老师,学习java,我学习其他语言的经验有限。

我做了自己的运动" noobish learner way"我不能帮助,但觉得这是一项拙劣的工作"当我看到"整洁&短"的解决方案。

然而,当我们一次又一次地提交两种解决方案时,我发现了#34;整洁"解决方案有点慢。

我依稀记得有关%操作成本高昂的事情,因为没有%操作,我的速度更快,还是有更多的操作不仅仅是那个?

练习:https://www.hackerrank.com/challenges/chocolate-feast

讨论中的解决方案:

import java.io.*;
import java.util.*;

public class Solution {
    static int cc; 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t,n,c,m,r;
            t = in.nextInt();
            while(t-->0){
             n = in.nextInt();
            c = in.nextInt();
             m = in.nextInt();
                r=n/c;
                cc=r;

                    while(r>=m){
                        cc=cc+r/m;
                        r=r%m+r/m;
                    }

                System.out.println(cc); 
            }

    }
}

我的解决方案:

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int t = Integer.parseInt(sc.nextLine());    //t = number of test cases
        int[][] tc = readInput(sc, t);              //tc[t][0] = money. tc[t][1] = price. tc[t][2] = wrappers per free bar

        for (int i = 0; i<t; i++){                  //loop for all test cases
            int choc = calcChoc(tc,i);              //work out how much choc can be bought
            System.out.println(choc);               //print result for the test case
        }
    }
    //calculate how much choc he can buy with m $ at p price with w wrappers needed for a free bar
    public static int calcChoc(int[][] tc,int i){

        int m = tc[i][0];       //money he has
        int p = tc[i][1];       //price of choc
        int w = tc[i][2];       //wrappers per free bar

        int bars = m/p;         //how many bars he can buy initially
        int wrappers = bars;    //each bar is a wrapper from initial purpose

        //loop to turn in all wrappers while it is possible to do so
        while (w<=wrappers){

            int barsFromTurnIn = wrappers/w;                //bars from turning in current wrappers.
            bars = bars + barsFromTurnIn;                   //new bar count
            wrappers = wrappers - (barsFromTurnIn * (w-1)); //wrapper count reduced by amount of wrappers turned in -1 wrapper per bar recieved from turn in.

            if (w==1){ //break out of infinite loop when you get 1 bar for 1 wrapper!
                System.out.print("Infinite Bars, exiting infinite loop at bars = ");
                break;
            }
        }
        return bars;
    }
    //read input for each test case and make 2d array of the info
    public static int[][] readInput(Scanner sc, int t){

        int[][] input = new int[t][3];

        for (int i = 0; i<t; i++){
            String[] inputLine = sc.nextLine().split(" ");

            input[i][0] = Integer.parseInt(inputLine[0]);
            input[i][1] = Integer.parseInt(inputLine[1]);
            input[i][2] = Integer.parseInt(inputLine[2]);
        }
        return input;
    }
}

1 个答案:

答案 0 :(得分:2)

我猜你的解决方案由于你读取输入数据的方法而更快:

public static int[][] readInput(Scanner sc, int t){

    int[][] input = new int[t][3];

    for (int i = 0; i<t; i++){
        String[] inputLine = sc.nextLine().split(" ");

        input[i][0] = Integer.parseInt(inputLine[0]);
        input[i][1] = Integer.parseInt(inputLine[1]);
        input[i][2] = Integer.parseInt(inputLine[2]);
    }
    return input;
}

比“整洁”解决方案中的in.nextInt()系列更快:

   n = in.nextInt();
   c = in.nextInt();
   m = in.nextInt();

如果我们查看java.util.Scanner的源代码,我们看到它使用regexp匹配来查找提供的输入流中的nextInt,而regexp匹配比在“space”字符上拆分一行要快得多解析3个整数:

public int nextInt(int radix) {
    // Check cached result
    if ((typeCache != null) && (typeCache instanceof Integer)
        && this.radix == radix) {
        int val = ((Integer)typeCache).intValue();
        useTypeCache();
        return val;
    }
    setRadix(radix);
    clearCaches();
    // Search for next int
    try {
        String s = next(integerPattern());
        if (matcher.group(SIMPLE_GROUP_INDEX) == null)
             s = processIntegerToken(s);
        return Integer.parseInt(s, radix);
    } catch (NumberFormatException nfe) {
        position = matcher.start(); // don't skip bad token
        throw new InputMismatchException(nfe.getMessage());
    }
}