logo
Tags down

shadow

Why my code is showing time limit exceeded


By : Brant
Date : September 16 2020, 05:00 AM
I wish did fix the issue. The original code tries to determine the number of hits required by summing the widths of the three blocks (w = w1 + w2 + w3;) and repeatedly dividing that by the strength S until the remaining width w becomes zero. If the strength S is 1 (and therefore w1, w2 and w3 are all 1 and their sum is 3), that will loop forever, causing the time limit for the code to be exceeded.
Also, it is not clear how the problem could be solved by division. Rather, the problem as stated involves subtraction, not division.
code :
if (s >= w1 + w2 + w3)
    hits = 1;
else if (s >= w1 + w2 || s >= w3 + w2)
    hits = 2;
else
    hits = 3;


Share : facebook icon twitter icon

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(x-1)); //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(x-1));
    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.
Related Posts Related Posts :
  • Recursion in c to print characters
  • Character array initialization using for loop
  • C Programming - Cannot Call Function More Than Once
  • Structures and pointer to pointer element
  • Error when initializing struct members in struct definition
  • Is that fixed-size array? a[]
  • It is not outputting what was expected and am unsure as to the issue
  • i want use C shell code to make buffer overflow and execute bin/sh
  • what the main function in this program returns to os?
  • Suffixes for integer types
  • I don't understand why a equals 1 and b equals 0 in this program
  • C prints other integer number than what it shows on Watches. Why is that?
  • Output of the for loop?
  • How to find 64-bit divisions in existing C code?
  • Finding a numbers with the largest/smallest number of bits set
  • how does " for (int j = 0; j < n || !putchar('\n'); j++) " work
  • handle ctrl+c in client-server socket in c programing
  • Restrict a process to read "/etc/resolv.conf" file in linux
  • Restrict scope of global variable inside one file in C
  • Arduino AES128 encryption - decryption problem
  • Why is not ampersand needed in the following code?
  • Is it possible to define a function name with #define?
  • Why is the size of the data type different when the value is directly passed to the sizeof operator?
  • Segmentation fault / Incorrect checksum triggered ocasionally when reading lines from a file
  • Adding bits at specific indexes for a uint8_t block
  • how can I use "shadow call stack" feature of clang-8 in x86_64?
  • malloc Assertion Failed
  • Array of structs inside of another struct in C
  • Why does this print the wrong part of the string?
  • Moving the cursor and printing characters in console doesn't work with sleep
  • Confusion in adding an element to a sorted list of integers
  • How I can separate integer number in 3 "houses"? Hundred, Ten and Unity
  • User-implemented memory management
  • What it entails to declare and initialize a pointer in this way: int (*p)[n]
  • Function to look though any struct in C
  • Microcontroller not recognized at USB port when connected via pins
  • Unexpected bitshift behavior in C
  • Model 3D spring from points
  • C control reaches end of non void function
  • Tax outputting 0
  • Gtk 3.0 Text View Resizable
  • Statically linking libc into my binary causes it to segfault
  • Child Process Executing Print Statement But Nothing Afterwards
  • Why is the function to find factorial wrong?
  • I am making a prime number checker: when I try to run it, it stopped working
  • I dont understand how this code can display a string by using just getchar
  • Traversing binary search tree, how to advoid code duplication
  • memcpy() overflow? Every eighth byte has its same bit pulled down when reading from FLASH
  • Warning: passing argument 2 of 'transform_labels' from incompatible pointer type [-Wincompatible-pointer-types]|
  • Is there any way i can print n lines with n columns where (1,1) is 1 ... (1,n) is n then (2,1) is 2 ... (2, n-1) is n an
  • Why am I getting some random values after the output?
  • C Program behaves differently if compiled using CLion rather than using gcc directly. Why?
  • Implementing CURL command using libcurl in C/C++
  • Changing number from character array to int array with decimal value
  • Finding certain words in a string in the loop
  • Warning C4047 : '=': 'int' differs in levels of indirection from 'int *'
  • Array with random numbers only contains the same values?
  • How to read line with space through redirection operator?
  • CLang-Tidy warning that rand() has limited randomness
  • format string '%g' requires an argument of type 'double', but variadic argument 1 has type 'long'
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk