All Questions
Tagged with python time-limit-exceeded
353
questions
8
votes
3answers
1k views
Find the number of trailing zeros in factorial
I am trying to find the "Number of trailing zeros of N!" (N factorial).
In the python code I am trying to find the factorial first and then the number of trailing zeros. However, I am ...
1
vote
1answer
59 views
Non equi self join with basic python libraries
I'm trying to do a non equi self join with basic python libraries on a table that has 1.7 millions rows and 4 variables. the data look like this:
...
1
vote
1answer
64 views
Google Foobar challenge optimization
I am trying to solve Google Foobar challenge prepare-the-bunnies-escape using my own approach, instead of BFS. I attempt to solve two ends of the maze and try to ...
4
votes
1answer
363 views
Find next smallest number that's representable as a sum of distinct powers of 3
I'm solving this challenge:
Given a positive integer number, you have to find the smallest good number greater than or equal to the given number.
The positive integer is called good if it can be ...
2
votes
2answers
80 views
Finding pair of numbers that minimize a function
This is a problem of a past bioinformatic contest (requires an account). My solution works but is too slow for some test cases.
Input format
The first line of the input contains one integer \$T\$, \$(...
1
vote
2answers
83 views
Compute members of the infinite series generated by 2n+1 and 3n+1
Consider the infinite sequence u, where u is defined as follows:
The number u(0) = 1 is the ...
2
votes
2answers
112 views
return the sum of the squares that is equal to n²
I'm struggling to solve this Kata. My code works but I can't pass because it's timing out. I have googled it and even tried other people's code but still doesn't pass.
Any suggestion on how can I make ...
3
votes
1answer
71 views
Java i/o speed less than python i/o while printing an array
Motivation: I was solving an array based i/o problem and encountered Time Limit Errors, it was later found that the code for java ran roughly 10x slower than the ...
0
votes
1answer
42 views
Return the prime numbers that are dividers of a some numbers in a list
What I need to do is to return the prime numbers that are dividers of a some numbers in a list, so if
I = [12, 15] # result = [[2, 12], [3, 27], [5, 15]]
and this ...
4
votes
1answer
54 views
Calculate probability that knight does not attempt to leave chessboard after at most k random moves (LeetCode #688)
I'm learning competitive programming and came across this question on LeetCode : 688. Knight Probability in Chessboard
On an n x n chessboard, a knight starts at the cell (row, column) and
attempts ...
0
votes
2answers
60 views
1
vote
2answers
72 views
Inserting data into MySQL
My code currently doesn't insert data into MySQL fast enough. However I don't know how to measure the current speed.
...
0
votes
5answers
91 views
Determine whether a magazine contains the words needed for a ransom note
This is a website Question on Hackrrank called Hash Tables: Ransom Note:
Given the words in the magazine and the words in the ransom note, print "Yes" if we can replicate the ransom note ...
-2
votes
2answers
75 views
Hackerrank: Absolute Permutation - How do I make this code more efficient
The problem is basically this: Return the permutation of the set of the first n natural numbers which satisfies the following property:
|pos(i) - i| = k
∀ i ∈ (1,n)
where pos(i) is the ith number in ...
1
vote
4answers
573 views
“Digit Queries” CSES Solution giving TLE
I really need a more accurate way to size inf_string. This code works but gets exponentially more inefficient the bigger max(queries) is, and therefore just can't hold up to the gargantuan numbers ...
2
votes
1answer
57 views
Performance issue in python- network creation based on the Euclidean distance with nested for loops is too slow
I want to create a network in which the links are formed based on a similarity metric defined as the Euclidean distance between the nodes. The distance is calculated using socio-demographic features ...
3
votes
2answers
136 views
How to speed up the code for LeetCode “Container with most water” task?
I'm trying to solve the LeetCode question where you need to find out the area of the container with the most water. I have created the solution that seems to work in my testing but it fails for being ...
4
votes
2answers
129 views
2
votes
1answer
139 views
Counting binary substrings
The exercise i am working on requires me to read a binary sequence stored in a text file. The objective is to find the occurrences of all subsequences of length between a and b (their values are given ...
11
votes
5answers
688 views
Finding reversed word pairs
I wrote this code to find opposites of words (i.e., words backwards). Input is a list of words. If this list contains a word + its opposite, then it should form a pair. The output should be a list of ...
1
vote
1answer
95 views
calculate tree height (arbitrary tree) - python
Given an array as input, we have to calculate the tree height. For example, input [4, -1, 4, 1, 1] means that there are 5 nodes with numbers from 0 to 4, node 0 is a child of node 4, node 1
is the ...
3
votes
2answers
2k views
Determine the longest segment of integers within an inclusive range that doesn't contain a bad number using Python
I am new to python.
Got a question in Hackerrank.
Given an Array of bad numbers and a range of integers, how can I determine the longest segment of integers within that inclusive range that doesn't ...
0
votes
2answers
73 views
Find the smallest triangular number with more than 500 factors
I wrote a program about triangular numbers, which are derived from the sum of natural numbers one after the other. For example, the third triangular number is equal to: 1 + 2 + 3 = 6.
I want to find ...
2
votes
1answer
63 views
Count the different positions reached in a cycle of permutations
I have come across a problem on cyclic permutations. It is about swapping numbers around.
It starts out with input N < 100,000 which makes the first N natural numbers as orders: 1 2 3 ... N-1 N
...
-3
votes
1answer
219 views
HackerRank | Roads and Libraries - Code Optimization
Major Edit: I wish people threw in a comment regarding what they are expecting instead of downvoting this question. I am quite appreciative of constructive feedback. I know it is only 3 people out of ...
2
votes
2answers
171 views
Determine whether a number and its square are both the sum of two squares
Code gets timed out and needs to be simplified.
To ensure that n = sum of squares
, To determine if n^2 = sum of squares
, I need to input 4 integers : 5881, 2048, 2670, 3482
...
3
votes
3answers
114 views
Summation of large inputs
Assume we are given \$ n \$ natural numbers, \$a_{1}, a_{2}, \dots, a_{n} \$ and we are supposed to calculate the following summation:
\$ \sum_{i = 1}^{n}\sum_{j = 1}^{n} \left\lfloor \dfrac{a_{i}}{a_{...
2
votes
5answers
103 views
Revised: Counting subnumbers divisible by a given prime
Assume we are given a number and a prime p. The goal is to count all subnumbers of number ...
2
votes
1answer
108 views
Dijkstra's algorithm in graph (Python)
I need some help with the graph and Dijkstra's algorithm in Python 3. I tested this code (look below) at one site and it says to me that the code took too long. Can anybody suggest how to solve that? ...
2
votes
1answer
106 views
Find minimal modulo-sum of pairs from two lists
Recently I appeared for an job challenge organised on HackerEarth.
Question:
There are two teams T1 and T2. Power of each player is represented in array. The rules of game are as follow.
There are ...
-4
votes
1answer
1k views
Removing chocolates 1 & 3 at a time
A box contains a number of chocolates that can only be removed 1 at a time or 3 at a a time. How many ways can the box be emptied?
The answer can be very large so return it modulo of 10^9+7
...
-1
votes
1answer
57 views
Find pairs of primes that sum to specified even number
Below code is for Given an even number (greater than 2), return two prime numbers whose sum will be equal to the given number
my task to reach the lowest time complexity
I tried it differently, but I ...
6
votes
1answer
94 views
Graph: Depth First Search (N citizens with pairs of friends)
I have solved problem 10608 on UVA Online Judge using Python 3.5.1. My solution works, but it takes too long to run when the online judge evaluates it.
Problem
There is a town with N citizens. It is ...
6
votes
1answer
151 views
01 Matrix is too slow when solved using DFS
I tried solving Leetcode 01 Matrix problem.
It is running too slow when solved using DFS approach.
Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
The distance ...
5
votes
1answer
60 views
Twitter contact scraper with Tweepy & Django
I created a project for academic purposes that works on Twitter data. It should:
Get all friends and follows of the user
Store them in MongoDB
Display them in a table
I'm using django + plain HTML, ...
2
votes
0answers
936 views
Better performance in solution for the Dole Out Cadbury challenge
Dole Out Cadbury
Problem Description
You are a teacher in reputed school. During Celebration Day you were assigned a task to distribute Cadbury such that maximum children get the chocolate. You have ...
7
votes
4answers
369 views
Finding the first pair of prime numbers that have some specified difference
I tried printing the first pair of prime number in between a list of numbers with a particular difference. But my run time is still too high (for large range of numbers). I want to reduce the run time ...
7
votes
1answer
73 views
“Jedi's riddle” challenge
The challenge can be found here.
In short we have a recursive function, and we need to find when \$z_i\$ is equal to one.
$$
\begin{align}
z_1 &= 1\\
z_i &= z_{i-1} * a\ \%\ n\\
\...
0
votes
0answers
34 views
How do I quickly look for parents containing their own code as child?
I have tree-like data that is constructed of Parent codes that contain Child codes which may act as parents themselves, depending on if they're marked as "SA". This data is present in an Excel sheet ...
2
votes
1answer
137 views
Binary Search implementation: HackerRank - Climbing the Leaderboard
My code passes 11 out of 12 test cases. I was wondering where I can improve my code. NOTE: This code needs performance improvement, as it is working for most of the cases. To mu knowledge it would ...
2
votes
1answer
226 views
Calculate your total happiness, which depends on the integers in the given sets [closed]
I am a beginner in programming. And I am learning python as my first language. I went on to do some challenges on HackerRank and stumbled upon this problem. The problem goes like this:
There is an ...
1
vote
1answer
202 views
Maximal Char Requests in a String
I have been trying to deal with this problem set, and I have figured out how to solve it as well. But there is one problem that is ...
5
votes
1answer
837 views
What should I do to reduce run time of my code?
This is a URI online judge problem (problem no: 1973).
Link : https://www.urionlinejudge.com.br/judge/en/problems/view/1973
After buying many adjacent farms at the west region of Santa Catarina,
...
3
votes
1answer
119 views
Checking which polygon a set of coordinates lie inside
I have a massive set of latitude, longitude pairs. I also have a list of GeoJson formatted locations. I want to find out which location each pair of coordinates lies in.
I have some code to do this, ...
1
vote
0answers
100 views
Wildcard Matching with DFS and FSM
I'm trying to solve Wildcard Matching on Leetcode. I found a great solution using the Finite-state machine. The author builds an FSM and searches with BFS.
I rewrite the solution with the same idea (...
0
votes
1answer
266 views
The Great Julya Calendar
Challenge
An integer is provided as an input. In each step, we have to subtract
a number from the input which already exists as a digit of the input.
In this way, we have to make the input ...
3
votes
1answer
158 views
Given an integer array A and a range, find a subarray within the range with a minimum sum
Problem
HackerRank Range query:
You are given an array A of n integers. You have to perform ...
3
votes
1answer
91 views
Flash Memory block write counting & thresholding
I am trying to solve this question:
In a block storage system, new data is written in blocks. We are going
to represent the flash memory as one sequential array. We have a list
of block writes ...
-4
votes
1answer
59 views
Geting maximum prime factor for a given number [closed]
I'm trying to solve a problem from Project Euler:
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?
and here is my attempt:
...
2
votes
1answer
83 views
python segment tree - count of smaller numbers after self
I am trying to solve this question https://leetcode.com/problems/count-of-smaller-numbers-after-self/
You are given an integer array nums and you have to return a new
counts array. The counts ...