edit: simplified solution. edit: removed opinion based secondary question.
background: atarted learning java week or 2 ago using hackerranks problems exercises , stackoverflow search + google teacher, i've had limited experience learning other languages.
i did exercise own "noobish learner way" can't feel "botched job" when see "neat & short" solutions.
however, when submitting both solutions 1 after couple of times found "neat" solution quite bit slower.
i vaguely remember % operations being costly, mine faster because of no % operations or there more that?
exercise: https://www.hackerrank.com/challenges/chocolate-feast
neat solution discussion:
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); } } }
my solution:
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 (int = 0; i<t; i++){ //loop test cases int choc = calcchoc(tc,i); //work out how choc can bought system.out.println(choc); //print result test case } } //calculate how choc can buy m $ @ p price w wrappers needed free bar public static int calcchoc(int[][] tc,int i){ int m = tc[i][0]; //money 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 can buy int wrappers = bars; //each bar wrapper initial purpose //loop turn in wrappers while possible while (w<=wrappers){ int barsfromturnin = wrappers/w; //bars turning in current wrappers. bars = bars + barsfromturnin; //new bar count wrappers = wrappers - (barsfromturnin * (w-1)); //wrapper count reduced amount of wrappers turned in -1 wrapper per bar recieved turn in. if (w==1){ //break out of infinite loop when 1 bar 1 wrapper! system.out.print("infinite bars, exiting infinite loop @ bars = "); break; } } return bars; } //read input each test case , make 2d array of info public static int[][] readinput(scanner sc, int t){ int[][] input = new int[t][3]; (int = 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; } }
i'd guess solution faster due method of reading input data:
public static int[][] readinput(scanner sc, int t){ int[][] input = new int[t][3]; (int = 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; }
being faster series of in.nextint() in "neat" solution:
n = in.nextint(); c = in.nextint(); m = in.nextint();
if @ source code of java.util.scanner see uses regexp matching finding nextint in supplied input stream , regexp matches quite bit slower spliting line on "space" characters , parsing 3 integers:
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 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()); } }
Comments
Post a Comment