patternjavaMinor
"Chef and Digits" Java solution
Viewed 0 times
chefdigitsjavaandsolution
Problem
I am looking for a review on the following code which is for this question. I am looking for a general review of the code. I am specifically also looking for advice on how to make this code run more efficiently.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.util.*;
public class Main
{
public static void main(String args[]) throws IOException
{
int S[] = new int[100000];
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
String s[] = in.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
String St = in.readLine();
char num[] = St.toCharArray();
for (int j = 0; j < m; j++)
{
String r1 = in.readLine();
int r = Integer.parseInt(r1);
int sum1 = 0, sum2 = 0;
for (int p = 0, len = num[r - 1]; p < r - 1; p++)
{
int diff = len - num[p];
if (diff < 0)
sum1 = sum1 + diff;
else
sum2 = sum2 + diff;
}
out.println((sum2 - sum1));
}
out.flush();
out.close();
}
}Solution
Naming Conventions
Variables in java should be
Meaningful names
Someone reading your code doesn't stand a chance in hell to understand what you are trying to do without actually running this code in his head...
Your class is called
Variable names like
Efficiency
It is very hard to give efficiency advice, if I can't say what exactly you are trying to achieve, but some obvious observations:
This doesn't look very efficient - you allocate all this memory up front, and I couldn't really see where you are using it...
Your is also not very defensive - you don't close your stream in case of an exception (you should use
Idioms
You should use the idioms relevant for the language you use, for example -
Another example - don't wrap
Variables in java should be
camelCase, meaning they should start with a small letter.Meaningful names
Someone reading your code doesn't stand a chance in hell to understand what you are trying to do without actually running this code in his head...
Your class is called
Main, your only method is main... at least try to tell your reader some story on what you are trying to do, especially when you post it for review!Variable names like
n, m, num, and sum add nothing to explain your code. Names like s and S are even worse.Efficiency
It is very hard to give efficiency advice, if I can't say what exactly you are trying to achieve, but some obvious observations:
int S[] = new int[100000];This doesn't look very efficient - you allocate all this memory up front, and I couldn't really see where you are using it...
Your is also not very defensive - you don't close your stream in case of an exception (you should use
try(PrintWriter out = new PrintWriter(System.out)) block to make sure of that), and you never validate that num[r - 1] is valid, so there is a decent chance that your code will not run as expected.Idioms
You should use the idioms relevant for the language you use, for example -
sum1 += diff; instead of sum1 = sum1 + diff;Another example - don't wrap
System.out with PrintWriter simply use System.out.println(sum2 - sum1)Code Snippets
int S[] = new int[100000];Context
StackExchange Code Review Q#46658, answer score: 6
Revisions (0)
No revisions yet.