Skip to main content

All Questions

59 votes
9 answers
14k views

Project Euler problem 1 in Python - Multiples of 3 and 5

I'd like suggestions for optimizing this brute force solution to problem 1. The algorithm currently checks every integer between 3 and 1000. I'd like to cut as many unnecessary calls to ...
Robert S Ciaccio's user avatar
17 votes
2 answers
2k views

Yandex programming contest: Alarms

I've tried to solve this challenge about 4 hours during the contest, but all my attempts exceeded the time limit. I tried to solve it with min-heap, but can't pass all the tests. How it can be solved ...
maksadbek's user avatar
  • 281
14 votes
5 answers
3k views

Advent of Code 2019: Day 4

Advent of Code 2019: Day 4 I'm doing Advent of Code this year. Below is my attempt at day 4: Problem Part One --- Day 4: Secure Container --- You arrive at the Venus fuel depot only to discover it's ...
Tobi Alafin's user avatar
  • 1,792
14 votes
1 answer
329 views

Recover flights from found tickets

I just found a whole bunch of plane tickets that only have a start and destination on them. I also know the route started in a certain city. I would like to recover the (alphabetically) first possible ...
redfast00's user avatar
  • 459
14 votes
1 answer
3k views

3-Sum Problem in Python

I attempted the 3-Sum problem on Leetcode, where the problem asks to find all possible triplets of numbers in a given list such that their sum is 0. My code worked, but it exceeded the time limit for ...
athul777's user avatar
  • 243
14 votes
3 answers
7k views

Maze solver and generator in Python

After watching Computerphile's video I decided to create my own maze solver and generator in Python. I've never written anything in Python so I'd like to get some feedback about my code, specifically ...
thjisisibot's user avatar
13 votes
5 answers
2k views

Array manipulation: add a value to each of the array elements between two given indices

This is a Hackerrank problem: https://www.hackerrank.com/challenges/crush/problem You are given a list of size \$N\$, initialized with zeroes. You have to perform \$M\$ operations on the list and ...
jeremy radcliff's user avatar
11 votes
3 answers
6k views

Algorithm for competing cells of 0s and 1s

I'm working on a practice algorithm problem, stated as follows: There are eight houses represented as cells. Each day, the houses compete with adjacent ones. 1 represents an "active" house and 0 ...
LuxuryMode's user avatar
11 votes
3 answers
2k views

Pigeon and grain problem: find time until all the grain is eaten

Problem: There are \$n\$ pigeons and \$m\$ grains. Pigeon \$i\$ eats a single grain every \$x_i\$ seconds. Find the time until all the grain is eaten. Input: A line with the integer \$...
Rohit Srivastava's user avatar
11 votes
1 answer
2k views

"Mixing Proteins" HackerRank challenge

I am doing this problem on HackerRank and I'm failing test cases because it's taking too long to run when n and k get over 8000. I believe the dictionary lookup should be \$O(1)\$ while lists are \$O(...
UnclosedParenthesis's user avatar
10 votes
4 answers
3k views

First non-repeating Character, with a single loop in Python

I recently tried to solve the first non-repeating character problem. Please tell me if my solution is valid. I'm aiming for O(n) with a single loop. My thinking is, it would be easy to tell you what ...
zing's user avatar
  • 203
10 votes
5 answers
869 views

Minimum perfect squares needed to sum up to a target

I'm trying to solve this problem Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. I've come up with a solution that works ...
nz_21's user avatar
  • 1,051
10 votes
2 answers
4k views

Generate all valid combinations of n pair of parentheses

Implement an algorithm to print all valid (e.g. properly opened and closed) combinations of n pair of parentheses. Following is my implementation and I think it works. Is the algorithm efficient? Do ...
Stack crashed's user avatar
10 votes
1 answer
1k views

Programming Challenge from Kattis: Apparatus

I am trying to solve apparatus problem, paraphrased below. There is an apparatus with n on/off switches with a one-to-one correspondence to n lights (but with an unknown permutation). We have ...
Ragnar's user avatar
  • 241
10 votes
2 answers
956 views

Finding all contiguous sublists such that each member appears an even number of times

The program needs to find all contiguous sublists of numbers where the number of occurrences of every member in the contiguous sublist is divisible by 2. The first line of input is N (the number of ...
Marko Mackic's user avatar

15 30 50 per page
1
2 3 4 5
11