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 ...
32
votes
Accepted
Analyzing Minesweeper Probabilities
GroupValues
GroupValues doesn't seem to serve much of a purpose beyond what you get from Map; it adds no functionality beyond ...
31
votes
Solution for kth row of Pascal's triangle for a job interview
For one thing, the inconsistency with whitespace use and curly brace placing may demonstrate a lack of attention to detail. Before you attack the actual problem, make sure your code is written ...
28
votes
Accepted
Solution for kth row of Pascal's triangle for a job interview
There are at least two optimizations you could make.
First of all, you are performing r multiplications and r divisions for ...
26
votes
Solution for kth row of Pascal's triangle for a job interview
Types are very important in C++.
You are being sloppy with your types.
long long sol; // big int object.
While you are putting it into an object that only ...
21
votes
Accepted
Count number of ways to paint a fence with N posts using K colors
1. Code review
The question says, "Write an algorithm that counts the number of ways" but the program does not only do this: it also builds a list of the combinations. This means that the program is ...
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?
...
19
votes
Solution for kth row of Pascal's triangle for a job interview
I am kind of curious why you separated the Int variable out of the for loop?
...
19
votes
Solution for kth row of Pascal's triangle for a job interview
As an interviewer, I'd see a big red flag in the implementation. Calculating each coefficient from scratch results in a lot of unnecessary recalculations.
Use recurrence: you already know \$\binom{n}{...
16
votes
Accepted
Generating Cartesian product of strings in R
Your approach is an example of good R code. However, there is a base function that allows creating a cartesian product of strings, interaction. This function ...
16
votes
Brute force password cracker
Expectation Setting
Your algorithm is an incrementing index, which you then convert in to the radix of your charset.
Your charset is what, 95 characters?
So, there are the following possible ...
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:
...
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
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 ...
16
votes
Accepted
List all possible permutations from a python dictionary of lists
The tool you need here is itertools.product:
...
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.
...
15
votes
Advanced and Detailed Minesweeper Probabilities
Focusing on optimizations:
in GroupValues:
...
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:
...
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 ...
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 ...
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 ...
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:
...
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
Analyzing Minesweeper Probabilities
One of the first rule I try to always follow is to be consistent. You're not always following the same pattern in your code for brackets.
...
13
votes
Accepted
Generate all possible combinations of letters in a word
I'm going to throw my hat in the ring with the iterative version of Heap's method of generating permutations. It took me a while to get right, as my reference source has a typo in the algorithm -_-.
<...
13
votes
Accepted
A beautiful and easy way of permutation with recursion
The simpler code would look something like this:
...
12
votes
Get distinct combinations of numbers
Let's talk algorithms, and complexity..... and what the actual problem is that you are trying to solve.
Background
Three concepts: distinct, permutation, and combination
Distinct
Distinct values ...
12
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
combinatorics × 480python × 145
algorithm × 127
java × 105
performance × 100
programming-challenge × 69
c++ × 67
recursion × 50
strings × 41
python-3.x × 37
time-limit-exceeded × 34
beginner × 33
c# × 29
javascript × 25
array × 23
c × 21
c++11 × 16
haskell × 15
python-2.x × 14
complexity × 13
interview-questions × 12
mathematics × 12
functional-programming × 9
reinventing-the-wheel × 9
comparative-review × 9