14 votes
Accepted

Find the Nth number divisible by only 2,3,5, or 7

No, not \$O(n)\$. Not even close. I'll get back to this later though. First, the code: It's a little difficult to tell what your loop logic is. curr is getting ...
  • 18.4k
13 votes

Performance of dynamic Fibonacci generator and dead rabbits

With regard to your time complexity: With your calculation of the nth Fibonacci number, you could do it in \$O(1)\$ time by using the relation: $$F_n = \left\lfloor \frac{\varphi^n}{\sqrt{5}} + \...
  • 3,770
12 votes
Accepted

DP example for "Power of Two" in Python

There are some major issues with your approach, the major one being that it is going to terribly break down for really large numbers. But if you want to go with it, here are a couple of improvements: ...
  • 6,562
12 votes

Yet another Fibonacci number generator

Assuming you're using the current c++ standard, since you don't specify in your question. Prefer stoi to atoi. ...
12 votes

Yet another Fibonacci number generator

Is this a generator or a calculator? Generators are objects that behave like iterators, yielding the next value on every call. ...
  • 6,138
11 votes
Accepted

Counting Increasing Subsequences of size K recursive

Concept You have called this a "Longest Increasing Subsequence" problem, but it's not, is it? It's counting all possible sequences of a specific length, not just the longest. As a result, I think you ...
  • 97.1k
11 votes
Accepted

Change-making problem with specific constraints

1. Review The function coinChange carries out three tasks: (i) read the input; (ii) solve the problem; (iii) print the solution. This makes it hard to test, ...
  • 49.4k
10 votes

DP example for "Power of Two" in Python

If someone were to say: isPowerOf2(9223372036854775808) what would your function do? For a start, it will end up with an array of a very, very large size.... that ...
  • 97.1k
10 votes
Accepted

Dynamic Programming: Fibonacci-like recurrence relation

Recursive Solution Code-wise, I see no issues. The one thing I'd point out is that it might be clearer to name your functions A and ...
  • 18.4k
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. ...
  • 9,581
9 votes
Accepted

Justify text from a file using a LaTeX method

You should have a look at Python's official style-guide, PEP8. One of its recommendations is to use PascalCase for class names and ...
  • 40.7k
8 votes

Coin Change: Minimum number of coins

Currently, your code is next to unreadable. The variable names are too cryptic. Names like den and S don't tell anything about ...
8 votes
Accepted

Coin Change: Minimum number of coins

Bug You code is currently too simplistic. All it does is make change from the highest denomination possible. It fails on the following input: ...
  • 28.4k
8 votes

Performance of dynamic Fibonacci generator and dead rabbits

The loop ...
  • 54.6k
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 ...
  • 5,966
8 votes
Accepted

Find smallest number of squares that sum to a number

Not an review, but an extended comment: I don't know how this solution can be optimized. The expected solution is radically different, and is based on a couple of theorems from number theory. You ...
  • 54.6k
8 votes

find longest common substring in space O(n)

PEP 8 You are following most of the PEP 8 style guidelines. One that you are breaking is method names should be snake_case; your function should be named ...
  • 32.9k
8 votes
Accepted

Printing the number of ways a message can be decoded

Your code performs one recursion per character and recursion depth is limited. For long inputs, your code will raise RecursionError: maximum recursion depth exceeded...
  • 2,317
8 votes

Recursive solution of ordered Coin Combinations II (CSES)

Normally I do a design review first, then review the code itself. This time I’m going to switch it around. Code review Before I get into review this code, I just have to say that the sample solution ...
  • 9,581
7 votes
Accepted

House-coloring optimization challenge

I do have a lot of comments, but I find your solution pretty good. The algorithm makes sense, and your comments make it easy to follow. The function is closer to a computation than a search, so I ...
7 votes

Yet another Fibonacci number generator

using namespace std using namespace std; Count the number of characters you use with this (twenty) and compare to the ...
  • 21.4k
7 votes

Obtaining a target number only using the operations ×2, ×3, and +1

For each i = 0...number, your method stores a std::list containing the complete sequence of intermediate numbers from ...
  • 22.7k
7 votes
Accepted

Project Euler #14: Longest Collatz Sequence starting point

Integer Division In Python, 10 / 2 is equal to 5.0, not 5. Python has the integer division ...
  • 32.9k
6 votes

Coin Change: Minimum number of coins

Everything is in main. It's called main. It's not called everything. You have written no ...
  • 25.2k
6 votes

Find the Nth number divisible by only 2,3,5, or 7

This Code Review, so let us review your code, before proceeding to questions and optimisations. Code and style review Choose better names – According to python style guide, PEP8, you should use <...
  • 11.5k
6 votes

Guess number with lower or higher hints

If I were Alice, the most optimum strategy, well, there are two. The binary search, or in Python is the bisection method, or I would use the Fibonacci method, which is mathematically superior and ...
6 votes

Recursive function and memorization to find minimum operations to transform n to 1

The problems you've encountered usually signal that the approach is not the best. Consider the very first step in your algorithm: you are definite that if the number is even, the best action is \$n\...
  • 54.6k
6 votes
Accepted

Dynamic programming solution to "Climbing Stairs"

The code in the post has to compute the \$i\$th Fibonacci number, \$F_i\$, for every \$i \le n\$, in order to compute \$F_n\$. It's possible to do much better than that, by using the recurrence $$ \...
  • 49.4k
6 votes

Find smallest number of squares that sum to a number

Review of your existing code (with some small performance improvements): nums = {} for x in range(1,n+1): nums[x] = 1 creates a dictionary with keys from <...
  • 22.7k
6 votes
Accepted

(Codewars Kata) Memoised Log Cutting

I see three problems with this function: Why calculate the values up to len(p), when you are actually interested in p[n] (which ...
  • 40.7k

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