17 votes

Dynamic programming with Fibonacci

A few things: Naming: It's neither Fibonaci, nor febonani, nor fibonanci it's ...
user avatar
  • 25k
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 ...
user avatar
  • 18.3k
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}} + \...
user avatar
  • 3,760
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: ...
user avatar
  • 6,512
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. ...
user avatar
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. ...
user avatar
  • 6,078
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 ...
user avatar
  • 96.8k
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, ...
user avatar
  • 49.2k
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 ...
user avatar
  • 96.8k
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 ...
user avatar
  • 18.3k
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. ...
user avatar
  • 8,666
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 ...
user avatar
  • 40.6k
8 votes

Dynamic programming with Fibonacci

@Vogel612 has already mentioned all major defects and areas of improvement in your code. I want to talk about one more thing: Your package naming is horrible. You are using ...
user avatar
  • 10.6k
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 ...
user avatar
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: ...
user avatar
  • 28.4k
8 votes

Performance of dynamic Fibonacci generator and dead rabbits

The loop ...
user avatar
  • 53.8k
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 ...
user avatar
  • 5,721
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 ...
user avatar
  • 53.8k
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 ...
user avatar
  • 31.2k
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...
user avatar
  • 2,277
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 ...
user avatar
  • 8,666
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 ...
user avatar
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 ...
user avatar
  • 20.8k
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 ...
user avatar
  • 22.4k
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 ...
user avatar
  • 31.2k
6 votes

Three-machine proportionate flow shop problem with unequal machine

Missing methods The posted code is missing the schedule_left() and CreateOutputFile() method. Style you should always ...
user avatar
  • 50k
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 ...
user avatar
  • 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 <...
user avatar
  • 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 ...
user avatar
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\...
user avatar
  • 53.8k

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