14 votes
Accepted

Flipping coins performance

The code you have posted is straightforward and basic. Because of this, I'm going to assume you are not super-experienced with Python. I'll make some direct suggestions to go from your current code to ...
aghast's user avatar
  • 12.3k
11 votes

Flipping coins performance

Thou shall not brute force. There is no reason to maintain the entire list of coin states, much less to perform actual flips. The state is easily recoverable from the sequence of ...
vnp's user avatar
  • 55.9k
11 votes

Flipping coins performance

I think the problem authors maybe intended for people to use a data structure that allows for fast queries and updates. You can make it run in \$Q\log(N)\$ time by using a tree structure. First note ...
Alex Selby's user avatar
10 votes

Merging Overlapping Intervals

This problem is well suited to an iterable method, rather than one that generates a list: ...
Widdershins's user avatar
10 votes

Collect subsequences of consecutive integers

@Peter Taylor has a good idea, but there is more to be improved You should add some tests Python is often described as Batteries included This is a perfect example to use ...
Ludisposed's user avatar
  • 11.5k
9 votes

Checking if two time intervals overlap

The code looks well presented, and it's clear how it maps to the problem statement. The one thing that's not well defined is how we deal with exact equality of times. I'm assuming that intervals are ...
Toby Speight's user avatar
9 votes
Accepted

Checking if two time intervals overlap

Negating the no-overlap case You mention four separate cases where two time intervals overlap. You then reduce it to three by pointing out that one case is really the intersection of two of the ...
mdfst13's user avatar
  • 21.4k
9 votes

Coding Challenge Solution - Good Range

Don't force a class where a function is perfectly fine. The namespace std is not designed for wholesale importation, see "Why is ...
Deduplicator's user avatar
  • 18.9k
8 votes

Checking if two time intervals overlap

Your code uses an algorithm that directly tests for an overlap of time spans, but a simpler algorithm is to check for a non-overlap - to check for whether the time-spans are completely distinct. The ...
elvaston5's user avatar
  • 134
8 votes

How to set an interval to std::map

Quite frankly it seems that you did not do what was asked from you. The text above assign gives you the requirements ...
miscco's user avatar
  • 4,231
8 votes

Modeling and computing intersection between continuous intervals

Review You are constrained to float which makes your class only usable for a limited number of scenarios. I would like to be able to work with ...
dfhwze's user avatar
  • 13.9k
7 votes

Finding the busiest time-frame in a parking lot

Naming Conventions MSDN, General Naming Conventions DO NOT use Hungarian notation: [dt]Start in getPickTimeframe() --> ...
Oguz Ozgul's user avatar
7 votes
Accepted

Collect subsequences of consecutive integers

Using yield is often simpler than building up a list to return. Here it would reduce ...
Peter Taylor's user avatar
  • 24.1k
7 votes

Finding all intervals that match predicate in vector

If you want to write concise, idiomatic C++, the best way is to rely on the STL as much as possible, as a tool as well as an inspiration. So how would this algorithm be implemented in the STL? It ...
papagaga's user avatar
  • 5,717
7 votes
Accepted

Finding all intervals that match predicate in vector

Code Review This piece ...
Incomputable's user avatar
  • 9,374
7 votes

Get consecutive integer number ranges from list of int

Your code takes a list, ensures it's sorted and distinct, then assembles and returns another list. This approach is problematic when the input is in fact a generator, in which case your code forces ...
Rainer P.'s user avatar
  • 2,447
6 votes

Converting a range of integers from a string to an IEnumerable

Ok, some comments: Code style While there are no definitive laws about code style, most C# programmers are putting the opening brace { on a separate line. If ...
Oskar Sjöberg's user avatar
6 votes
Accepted

Searching the number with the biggest amount of odd divisors in an interval

Bug Your code isn't always correct: >>> amountOddDivisorsOf(15) 3 Should be 4. Coding style The Python style guide recommends ...
Barry's user avatar
  • 18.4k
6 votes

Flipping coins performance

Since Codechef seems to support numpy, I would propose a solution like this: ...
Graipher's user avatar
  • 40.8k
6 votes

Flipping coins performance

It looks like certain numpy operations give a very large constant factor speed benefit, as Graipher demonstrated in his answer. Graipher's solution is \$O(QN)\$ but is competitive with the \$O(Q\log(N)...
Alex Selby's user avatar
6 votes

Traffic speed limit check for intervals

First, let's look at your answer with comments: ...
Daniel's user avatar
  • 171
6 votes
Accepted

Find the intervals which have a non-empty intersection with a given interval

1. Review There is no docstring. What does the function find_intersections do? The text in the post is pretty clear, so you could use this as the basis of a ...
Gareth Rees's user avatar
  • 49.6k
6 votes
Accepted

Counting integers that are within intervals

If you sort your values, you can then make an iterator on the sorted list, forward it to the lower limit, count until the first value is reached that is larger than the upper limit and discard all ...
Graipher's user avatar
  • 40.8k
6 votes
Accepted

Get consecutive integer number ranges from list of int

Your code is really complicated. First, your code should be abstracted a little more. It is not specific to feature IDs, therefore the terminology should not use these words. The same algorithm can be ...
Roland Illig's user avatar
  • 21.2k
5 votes
Accepted

"Can a list of meetings all be scheduled in a conference room?"

Like knut suggested, a simple solution would be to sort the meetings by start time and for each check if it conflicts with the next. E.g. given a list of meetings: ...
Flambino's user avatar
  • 33k
5 votes
Accepted

Counting the overlapping intervals in the union of two sets

Bugs I tried this test input: int[] a = {1, 10, 30, 60 }; int[] b = {2, 20, 40, 70 }; and your program returned 3. ...
JS1's user avatar
  • 28.5k
5 votes

Checking overlap ranges between two prices

I am adding my comments at first glance. This is not a final review as this code needs lots of refactoring. Delete commented code Never keep commented code; it adds distraction while reading by ...
Paritosh's user avatar
  • 2,638
5 votes
Accepted

Representing and Parsing an Open or Closed Range

I would check the result of range range = _RemoveSpaces.Replace(range, string.Empty); against string.Empty because that is ...
Heslacher's user avatar
  • 50.3k
5 votes
Accepted

Add dash between more than two consecutive numbers and comma between other numbers in array

Sorry but no comments (yet) on the LINQ. Your first conditional has a logic error. You should be checking if orderedArray is null before you check if its length ...
Rick Davin's user avatar
  • 6,217
5 votes

Find the intervals which have a non-empty intersection with a given interval

return value In case there is no overlap, it would be cleaner to either return a sentinel value (None for example), or raise an exception, and don't trigger any ...
Maarten Fabré's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible