UVa 11616 Roman Numerals Time Limit Exceeded  I am receiving Time Limit Exceeded consistently
By : Nora Blake
Date : March 29 2020, 07:55 AM
hop of those help? I solved my problem by going about it in a different manner, I used a couple of HashMaps to map Roman numeral values to Arabic numeral values and vice versa. I had four helper methods: one would set up the hashmaps, another would convert from Roman numeral to Arabic numeral, and the other two would work together to convert from Arabic numeral to Roman numeral. The method that converted from Roman to Arabic would go through the string in a for loop starting from the beginning of the string. It would check if the length of the string was greater than one, and if so it would then check if the substring of the first two values are in the Roman to Arabic hashmap. If so, it would then add the value that the Roman numeral value equates to to an int variable. The method would also check substrings of length 1. code :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
class Main {
private static HashMap<String,Integer> r2A = new HashMap<String,Integer>();
private static HashMap<Integer,String> a2R = new HashMap<Integer,String>();
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
makeMaps();
String read;
StringBuilder answers = new StringBuilder("");
while ((read=br.readLine())!=null) {
int aNum = 0;
String rNum = "";
try {
aNum = Integer.parseInt(read);
System.out.println(arab2Roman(aNum));
} catch (NumberFormatException c) {
rNum = read;
int ans = roman2Arab(rNum);
System.out.println(ans);
}
}
}
private static int roman2Arab(String rNum) {
int aNum = 0;
for (int i = 0; i < rNum.length(); i++) {
boolean done = false;
String theNum = rNum.substring(i,i+1);
if (i < rNum.length()1) {
String part = rNum.substring(i, i+2);
if (r2A.containsKey(part)) {
aNum+=r2A.get(part);
i++;
done = true;
}
}
if (!done) {
if (r2A.containsKey(theNum)) {
aNum+=r2A.get(theNum);
}
}
}
return aNum;
}
private static String arab2Roman(int num) {
StringBuilder rNum = new StringBuilder("");
int thousands = num(num%1000);
int hundreds = ((num/100)%10)*100;
int tens = ((num/10)%10)*10;
int ones = num%10;
rNum.append(simpleConv(thousands,"thousands"));
rNum.append(simpleConv(hundreds,"hundreds"));
rNum.append(simpleConv(tens,"tens"));
rNum.append(simpleConv(ones,"ones"));
return rNum.toString();
}
private static String simpleConv(int num, String place) {
StringBuilder ans = new StringBuilder("");
int pNum = (place.equals("thousands")) ? 1000 : (place.equals("hundreds")) ? 100 : (place.equals("tens")) ? 10 : 1;
if (a2R.containsKey(num)) {
ans.append(a2R.get(num));
}
else {
if (num/pNum>=5) {
ans.append(a2R.get(5*pNum));
for (int i = 0; i < ((num/pNum)5); i++) {
ans.append(a2R.get(pNum));
}
}
else {
for (int i = 0; i < num/pNum; i++) {
ans.append(a2R.get(pNum));
}
}
}
return ans.toString();
}
private static void makeMaps() {
// First r2A
r2A.put("I", 1);
r2A.put("IV", 4);
r2A.put("V", 5);
r2A.put("IX", 9);
r2A.put("X", 10);
r2A.put("XL", 40);
r2A.put("L", 50);
r2A.put("XC", 90);
r2A.put("C", 100);
r2A.put("CD", 400);
r2A.put("D", 500);
r2A.put("CM", 900);
r2A.put("M", 1000);
// Second a2R
a2R.put(1, "I");
a2R.put(4, "IV");
a2R.put(5, "V");
a2R.put(9, "IX");
a2R.put(10, "X");
a2R.put(40, "XL");
a2R.put(50, "L");
a2R.put(90, "XC");
a2R.put(100, "C");
a2R.put(400, "CD");
a2R.put(500, "D");
a2R.put(900, "CM");
a2R.put(1000, "M");
}
}

Why am I getting Time limit exceeded for my this code?
By : user3722528
Date : March 29 2020, 07:55 AM
this will help Why am i getting Time limit exceeded for ans for my this code? I tried this question on CodeChef.My logic is correct but my answer is showing time limit exceeded , i don't know why ? , Please read the question correctly 

proplem of Memory limit exceeded and Time limit exceeded in python
By : Maxim Ch
Date : March 29 2020, 07:55 AM
will be helpful for those in need i have a problem to solve on the online judge its solution is to make the sum of the integer for example input 4 so 1+2+3+4 the output 10 and for another example input 10 so get sum of 1 to 10 to get in the output 55 and so on but when i used List to stor numbers and make sum(List) its give me Memory limit exceeded here is the code code :
This is arithmetic progression and computes as S = ½(a1 + an)n,
where a1 is first member, which is 1 in this case. an is the last
member which is n in this case.
def arthimPSum(n):
return round((1 + n)*n *0.5)
print(arthimPSum(10**9))
500000000500000000

Why it is showing time limit exceeded?
By : Codrin Bălăucă
Date : March 29 2020, 07:55 AM
this will help Problem is that your fact function never stopped since while loop never ends. code :
int fact(int x)
{
int ans;
while(x!=1)
ans=(x*fact(x1)); //X is never changed!
return ans;
}
int fact(int x)
{
int ans = 1; //Set default value for return
if(x!=1) //Go recursive only if X != 1
ans=(x*fact(x1));
return ans;
}

Time limit exceeded in my code given below
By : Michael K
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further For each of O(n) characters you do a O(n) search leading to a O(n^2) algorithm. Throw a thousand character string at it, and it is too slow. This is solvable in two standard ways. The first is to sort each half of the string and then compare. The second is to create hash tables for letter frequency and then compare.

