Hi ,
can you please tell me what is wrong with this code . This is the java code for
‘Bytelandian gold coins’ practice problems . My output is correct and within the time limit also. But not sure why I am getting Runtime error
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
class GoldCoin {
static HashMap<Long,Long> cached = new HashMap<Long,Long>();
public static void main(String[] args)
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ArrayList b = new ArrayList();
int count=0;
String row;
while(count<10)
{
try {
row=br.readLine();
if(isInteger(row))
{
b.add(Long.parseLong(row));
count++;
}
else
break;
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (Long k:b)
{
System.out.println(getDollar(k));
}
}
static long getDollar(Long k)
{
long m;
if(cached.containsKey(k))
return cached.get(k);
else
m=(k > (k/2 +k/3+k/4))? k :(getDollar(k/2) +getDollar(k/3)+getDollar(k/4));
cached.put(k,m);
return m;
}
static boolean isInteger(String s) {
try {
Long.parseLong(s);
} catch(NumberFormatException e) {
return false;
}
// only got here if we didn't return false
return true;
}
}