17
votes
Accepted
Get all combinations of selecting k elements from an n-sized array
Binomial coefficient \$\binom{n}{k}\$ is growing too fast (with \$n\$) to keep a list of all combinations in memory. I would suggest to use yield return instead.
...
15
votes
Determine if one string is a permutation of the another
I would say yes, use an array.
Arrays afford very good memory locality, which is good for cache performance. They essentially are the most basic and efficient hash map if the key is the index.
You ...
14
votes
Accepted
Determine if one string is a permutation of the another
Comparing two maps is linear, so that's not a big problem.
I don't think you've made particularly good use of maps though. As I see it, you have two choices. You could use an ...
13
votes
Accepted
A beautiful and easy way of permutation with recursion
The simpler code would look something like this:
...
13
votes
Accepted
Earliest time frog can jump to the other side of a river in C#
Readability
The code needs more spacing. Considering the comments, the indentation and the ifs, it's hard to read without some good old empty lines.
Use brackets when using conditions, especially if ...
12
votes
Accepted
Generating Permutations in Python
Python has so many neat stuff to help you with this one, I feel alot can be rewritten:
I did a lot of C++ and miss std::swap(..)
Luckily you can swap really easy in python
For instance the swap ...
10
votes
Project Euler #15: counting paths through a 20 × 20 grid
#include <cstdio>
If your goal is to write C++, you're really off to a bad start using the C standard I/O library.
...
10
votes
Generate Letter Combinations of a Phone Number
You can set the keypad lookup as a constant instead of computing it dynamically. Setting it up as constant makes it clearer that what your intent is.
...
10
votes
HackerRank: Electronics Shop
early pruning
This is very nice:
// delete any that are over our budget
Doing it before sorting can slightly speed the sorting operation.
I say slightly ...
10
votes
Print sums of all subsets
Preface
This is great code. Your solution is more than \$10^{42}\$ times nicer than the given solutions on the linked page that promote crap like ...
10
votes
Accepted
LINQ for generating all possible permutations
English language
These are relatively minor issues, but fixing them might help other people to use / maintain your code.
The verb corresponding to permutation is permute.
I'm pretty sure that ...
10
votes
Accepted
Print all "balanced" sequences of 'A' and 'B'
Refactor to use functions
Try to avoid boolean variables. Pseudocode like:
...
9
votes
Accepted
Maximum sub-array of non-negative numbers
1. Bugs
maxset returns the wrong answer for some inputs. For example:
>>> maxset([2, 1, -1, 1, 3])
[2, 1]
The ...
9
votes
Refactor the code which performs "cross-product", "reduce", "product of list" and "sum of list"
One immediate improvement is realizing that for e.g the input 14 7, all combinations like x x x x x x 9 and above can never sum ...
9
votes
Find all combinations of length 3 whose sum is divisible by a given number
Time complexity of iterating through all the combinations is O(n³).
We can solve it in O(n²) by using the fact that if you know 2 numbers out of 3, you know exactly ...
8
votes
Generating Permutations in Python
Are you allowed to use standard library functions? If so, why not just use itertools.permutations():
...
8
votes
Accepted
Project Euler #15: counting paths through a 20 × 20 grid
Why are you using unsigned short?
I presume the answer is somewhere along the lines of worrying about memory usage. There isn't a problem with that, but given that ...
8
votes
Generating the powerset in C
I'll just work through this from the top:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Looks good; we need these.
...
8
votes
HackerRank: Electronics Shop
General Guidelines
You have coded everything in a single class Program. Take advantage of the fact C# is an object oriented language. Create at least one custom ...
8
votes
Accepted
HackerRank: Electronics Shop
3 good answers already, but there's more!
I don't like that you modify the array you are given. This sort of thing would need to be documented, and generally creates confusion for all. You don't need ...
8
votes
LINQ for generating all possible permutations
Using line breaks and {} in code is not a crime :-P Only because we have the nice => doesn't mean we have to use it ...
8
votes
Accepted
Codility's Permutation Check in C#
One of the unfortunate things you'll notice about coding challenges is that the requirements often force you to write code that isn't ideal. I'm going to call these out, even though in this case they'...
8
votes
Letter Combinations of a phone number using a Dictionary
Review
LetterCombinations returns an IList<string> but I see no reason to return a modifiable collection. Consider ...
7
votes
Generating Permutations in Python
A miscellaneous improvement:
To make the pattern in your first for-loop cleaner, Python offers for–...
7
votes
A beautiful and easy way of permutation with recursion
I find permutations both fun and fascinating, so this is pretty cool.
I was going to say most of what @Jerry Coffin said, so rather than repeat all that, I'll just point out a few things:
Your ...
7
votes
Accepted
Generalized Cartesian Product
Naming
Using m_x for the container (in product) as well as the iterator (in product_iterator...
7
votes
Accepted
Checking if an integer permutation is cyclic in Java
Here
int nextNumber = array[array[0]];
int visitedNumbers = 1;
while (nextNumber != array[0]) {
nextNumber = array[nextNumber];
visitedNumbers++;
}
you ...
7
votes
Accepted
PermCheck Codility
Summing the arrays is not necessary. You only need to check that the input's maximum and length are equal, and that it's free of duplicates.
This approach scores 100% as well. It saves a couple of ...
7
votes
Print sums of all subsets
This is bad. You calculate them all first, and only then print them out. And what if n = 20, or 42, or 100? The printout will never start (and the memory will blow up before that, too).
Instead, have ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
combinatorics × 500python × 157
algorithm × 134
java × 110
performance × 100
programming-challenge × 71
c++ × 66
recursion × 49
strings × 42
python-3.x × 38
time-limit-exceeded × 37
beginner × 34
c# × 29
javascript × 25
array × 22
c × 21
c++11 × 16
python-2.x × 15
haskell × 15
mathematics × 14
complexity × 13
interview-questions × 12
functional-programming × 9
comparative-review × 9
game × 8