Home Dynamic Programming I | Study Plan | Leetcode
Post
Cancel

Dynamic Programming I | Study Plan | Leetcode

Day 1 | Array

509. Fibonacci Number

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

1
2
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.

Given n, calculate F(n).

Recursion

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
    int fib(int n) {

        if(!n) return 0;
        if(n == 1 or n == 2) return 1;

        vector<int> dp(n);

        dp[0] = dp[1] = 1;

        for(int i=2; i<n; i++)
            dp[i] = dp[i-1] + dp[i-2];

    return dp[n-1];
    }
};

DP

1
2
3
4
5
int fib(int n) {
	if(n <= 1)
		return n;
	return fib(n - 1) + fib(n - 2);
}

Binet’s Formula

Binet’s formula for the n’th Fibonacci number:

loading image

This formula can compute the solution in O(1) time and O(1) space.

This implementation breaks down after n > 71 because Math.pow uses floating point numbers to be able to do exponents in O(1) time.

1
2
3
4
5
6
7
class Solution {
public:
    int fib(int n) {
        double sqrt5 = sqrt(5);
        return (pow(1 + sqrt5, n) - pow(1 - sqrt5, n)) / pow(2, n) / sqrt5;
    }
};

Golden Ration

This formula can compute the solution in O(logn) time and O(1) space.

This implementation breaks down after n > 71 because Math.pow uses floating point numbers to be able to do exponents in O(logn) time.

1
2
3
4
int fib(int n) {
	double goldenRatio = (1 + sqrt(5)) / 2;
	return round(pow(goldenRatio, n) / sqrt(5));
}

1491. Average Salary Excluding the Minimum and Maximum Salary

The Tribonacci sequence Tn is defined as follows:

T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.

Given n, return the value of Tn.

Recursion

1
2
3
4
5
6
7
8
9
class Solution {
public:
    int tribonacci(int n) {
        if(n<2)return n;
        if(n==2)return 1;

        return tribonacci(n-1)+tribonacci(n-2)+tribonacci(n-3);
    }
};

DP

1
2
3
4
5
6
7
8
9
class Solution {
public:
    int tribonacci(int n) {
    int dp[3] = {0, 1, 1}4;
    for (int i = 3; i <= n; ++i)
        dp[i%3] += dp[(i+1)%3] + dp[(i+2)%3];
    return dp[n%3];
    }
};

Day 2

70. Climbing Stairs

You are climbing a staircase. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Recursion

1
2
3
4
5
6
class Solution {
public:
    int climbStairs(int n) {
    return n <= 2 ? n : climbStairs(n-1) + climbStairs(n-2);
    }
};

DP

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
    int climbStairs(int n) {
    if(n <= 2) return n;
    int * dp = new int[n];
    dp[0] = 1, dp[1] = 2;
    for(int i = 2; i < n; i++)
        dp[i] = dp[i-1] + dp[i-2];

    return dp[n-1];
    }
};

746. Min Cost Climbing Stairs

You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.

You can either start from the step with index 0, or the step with index 1.

Return the minimum cost to reach the top of the floor.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        int n=cost.size();
        int tmp, first=cost[0],second=cost[1];
        for(int i=2;i<n;i++){
            tmp=min(first,second)+cost[i];
            first=second;
            second=tmp;
        }
        return min(first,second);
    }
};
This post is licensed under CC BY 4.0 by the author.

Google Hashcode 2022

Programming Skills I | Study Plan | Leetcode