Giter VIP home page Giter VIP logo

recursive-dynamic-programming's Introduction

Recursion & Dynamic Programming

In this assignment you will practice writing recursion and dynamic programming in a pair of exercises. There is also an optional harder followup to the second exercise.

Learning Goals

In this exercise you will

  • Practice writing recursive methods
  • Practice using dynamic programming techniques
  • Determine time & space complexities of recursive methods

Improved Fibonacci

Earlier you saw how an array could be used to store Fibonacci numbers resulting in a time-complexity reduction from O(2n) to O(n). Now we will take this a set further, because to find a given Fibonacci number, you only need to find the previous two numbers.

Reminder:

Fib(0) = 0 Fib(1) = 1

Fib(n) = Fib(n-2) + Fib(n-1), for all n >= 2

Restrictions:

  • You cannot use a loop, use recursion.
  • Your solution should be O(n) space complexity, due to the call stack.

Superdigit

We define super digit of an integer using the following rules:

Given an integer, we need to find the super digit of the integer.

If the number has only digit, then its super digit is that number. Otherwise, the super digit of x is equal to the super digit of the sum of the digits of x. For example, the super digit of 9875 will be calculated as:

super_digit(9875) --> superdigit(9 + 8 + 7 + 5) = superdigit(29)
super_digit(29)   --> superdigit(2 + 9) = superdigit(11)
super_digit(11)   --> superdigit(1 + 1) = superdigit(2)
super_digit(2) --> 2

So the super_digit of 9875 is 2.

Refined Super Digit - Optional

In this exercise we will build on the Superdigit concept. A refined superdigit is a determined by concatenating a numbrer n a specific k number of times and then calculating the superdigit.

For example if k = 3 and n = 148

refined_superdigit(n, k) = superdigit(148148148) 
= superdigit(1 + 4 + 8 + 1 + 4 + 8 + 1 + 4 + 8) 
= superdigit(39) 
= superdigit(3 + 9) 
= superdigit (12) 
= superdigit (1 + 2)
= 3

You can use your superdigit solution in the solution for refined_superdigit. Can you reduce the time complexity?

Problem Source

recursive-dynamic-programming's People

Contributors

cheezitman avatar roshni-patel avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.