39 votes
Accepted

Counting permutations without repetitions for a number or a string

One of the best ways to make a program faster is not to compute things that you don't have to. In this question you are asked to compute the number of permutations. But your implementation goes and ...
  • 49.4k
20 votes
Accepted

Brute Force Algorithm in C

I'm going to skip reviewing the OP's code, which has already been reviewed quite nicely by others. Instead I'm just going to show a much faster version and explain it. What is the algorithm? ...
  • 28.5k
16 votes

Advanced and Detailed Minesweeper Probabilities

Focusing on optimizations: in GroupValues: ...
  • 22.2k
16 votes

Pairwise combinations of an array in Javascript

Pairing combinations like this is a very common thing to do in any language. There's a very 'idiomatic' way to do this type of operation: ...
  • 97k
16 votes
Accepted

Calculate all possible distributions of n objects to k containers

The structure of the code makes it difficult to test. Suppose you had two different implementations, and wanted to test that they returned the same results? The code doesn't lend itself well to this ...
  • 16.2k
16 votes
Accepted

Printing all possible sums of two n-sided dice rolls

As @BenC mostly put a code dump, I'll explain ways that your code could have improved. reverseIt Programming this is just wrong in Python. This is as there are ...
  • 41.8k
16 votes
Accepted

List all possible permutations from a python dictionary of lists

The tool you need here is itertools.product: ...
  • 49.4k
16 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. ...
  • 912
15 votes
Accepted

Approximate (250 over 100) permutation best fitting certain criteria

1. Analysis In the general case, the problem of finding the sample of words with the "most even" distribution of letters is NP-hard. Here I'm considering a general instance of this problem to be: ...
  • 49.4k
15 votes
Accepted

Generate a dictionary of passwords

Please take a look at itertools.product. It will simplify your code greatly. ...
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 ...
  • 5,936
14 votes
Accepted

Pairwise combinations of an array in Javascript

Your solution works fine, and it's fairly efficient. You're using loops and mutation, which are speedy, but also not terribly readable. This algorithm can be expressed more functionally in terms of ...
  • 3,172
14 votes
Accepted

Minimising the triangle

Style In Java it is common practice to have class names to use PascalCase (a.k.a. UpperCamelCase). Also the name spaceship doesn't make sense to me and is ...
  • 16.5k
14 votes

Counting permutations without repetitions for a number or a string

You're looking for set(). The reason for your program being slow is: you're calculating the permutation once: ...
14 votes
Accepted

Find the total number of ways W, in which a sum S can be reached in N throws of a dice

1. Review The sum \$s=0\$ can be reached in \$n=0\$ throws in exactly one way, but: ...
  • 49.4k
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 ...
  • 33.3k
13 votes
Accepted

Advanced and Detailed Minesweeper Probabilities

@Pimgd were definitely on to something big in the last part of the answer, but not on to it enough. Simply changing Solution<T> to ...
13 votes
Accepted

A beautiful and easy way of permutation with recursion

The simpler code would look something like this: ...
  • 33.3k
12 votes

TopCoder problem "Lottery" - SRM 144 (Division I Level Two)

Style Dead code should be removed. Naming single letter variables should be avoided if they are not temporary. variable names should be named using ...
  • 50.1k
12 votes

Printing all possible sums of two n-sided dice rolls

For your specific case of two rolls of n-sided dice: ...
  • 2,738
12 votes
Accepted

isPermutation method

First off: bool isPermute(char*, char*); char* sort(char*); You could just move the function implementation over the main method, avoiding to have these ...
  • 274
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 ...
  • 11.5k
12 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.3k
11 votes

Brute Force Algorithm in C

There are some things I've seen that may help you improve your code. Prefer iterations to recursion Recursive functions are often a good way to approach a programming task, but there is a tradeoff ...
  • 64.9k
11 votes

Generating all combinations of an array

In this case, a non-recursive solution is easier to understand, IMHO: ...
11 votes
Accepted

TopCoder problem "Lottery" - SRM 144 (Division I Level Two)

Priority queue There are warnings of unchecked calls, mostly since you declared MinPriorityQueue<Key extends Comparable, Value extends Comparable>. ...
11 votes

Printing all possible sums of two n-sided dice rolls

I've got a suggestion to an alternative way of generating your list based on your own thoughts on how the sequence grows. But first a few general remarks related to the style of your code. Naming of ...
  • 11.5k
11 votes
Accepted

On Knuth's "Algorithm L" to generate permutations in lexicographic order

Optimising the functional approach Iterative vs functional approach: Is it possible to use the (existing) functional methods without losing performance? I don't think it's possible to use the ...
  • 423
11 votes
Accepted

Print all "balanced" sequences of 'A' and 'B'

Refactor to use functions Try to avoid boolean variables. Pseudocode like: ...
  • 3,242
10 votes

Random permutation of int[] and ArrayList<Integer>

Use for-each Use for-each idiom to loop over an array when possible, and you can drop this comment: ...
  • 110k

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