1. Take the Stairs Suppose you are climbing the stairs. It takes n steps to get to the top. You can climb one or two steps at a time. How many different ways can you climb to the top

function climbStairs(n) {
       let dp=[];
       dp[0] =0;
       dp[1] =1;
       dp[2] =2;
       for(let i=3; i<n; i++){ dep[i]=dep[i-1]+dp[i-2];
       }
       return dep[n];
 }
Copy the code

You are a professional thief and plan to rob the houses along the street. There is a certain amount of cash hidden in each room, and the only constraint on your ability to steal is that adjoining houses are equipped with interconnected security systems that will automatically alert the police if two houses are broken into on the same night.

Given an array of non-negative integers representing the amount stored in each house, calculate the maximum amount you can steal without setting off the alarm

function rob(nums){
    sum1=nums[0];
    sum2=nums[1];
    for(let i=2; i<nums.length; i++){lettemp=sum1; sum1=sum1>sum2? sum1:sum2; sum2=temp+nums[i]; }returnsum1>sum2? sum1:sum2 }Copy the code

3. Largest square In a two-dimensional matrix consisting of 0 and 1, find the largest square containing only 1 and return its area

function maxSquare(matrix){
   let row=matrix.length;
   let column=matrix[0].length;
   let dp=new Array(row+1).fill(0).map(() = >new Array(column+1).fill(0))let maxLen=0;
   for(let i=1; i<row; i++){for(let j=1; j<column; j++{if(matrix[i-1][j-1] = =1){
               dp[i][j]=Math.min(dp[i-1][j],dp[i-1][j-1],dp[i][j-1]) +1;
               maxLen=Math.max(maxLen,dp[i][j]; }}}return maxLen*maxLen;
}
Copy the code

Exchange coins of a given denomination and an amount. Write a function to calculate the minimum number of coins needed to make up the total. If no one coin combination makes up the total, return -1

function coinChange(coins, amount) {
    if(amount === 0) return 0;
    // Initialize an array with an amount+1 length and an infinite value
    let dp = Array(amount+1).fill(Infinity)
    dp[0] = 0;
    for(let i = 0; i < dp.length; i++) {
        for (let j = 0; j < coins.length; j++) {
            // If the difference is less than zero
            if (i - coins[j] < 0) continue;
            // A minimum of several coins is required for each amount
            dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1); }}if(dp[amount] === Infinity) return -1;
    else return dp[amount] 
};
Copy the code

5. Different paths A robot is located in the upper left corner of an M x N grid (the starting point is marked as “Start” in the figure below). The robot can only move one step to the right or down at a time. The robot attempts to reach the lower right corner of the grid (marked “Finish” in the image below).

I’m asking you how many different paths there are

function uniquePaths(m, n) {
    const f = new Array(m).fill(0).map(() = > new Array(n).fill(0));
    for (let i = 0; i < m; i++) {
        f[i][0] = 1;
    }
    for (let j = 0; j < n; j++) {
        f[0][j] = 1;
    }
    for (let i = 1; i < m; i++) {
        for (let j = 1; j < n; j++) {
            f[i][j] = f[i - 1][j] + f[i][j - 1]; }}return f[m - 1][n - 1];
};
Copy the code

Given an array prices, the ith element of prices[I] represents the price of a given stock on the ith day.

You can only buy the stock on one day and sell it on a different day in the future. Design an algorithm to calculate the maximum profit you can make.

Return the maximum profit you can make from the transaction. If you can’t make any profit, return 0

function maxProfit(prices) {
    let result = 0
    let len = prices.length
    let minPrice = Infinity

    for(let i=0; i < len; i++){
        // With current prices below record lows, buy here
        if(prices[i] < minPrice){
            minPrice = prices[i]
        // If current gains are greater than historical highs, sell here
        }else if (prices[i] - minPrice > result){
            result = prices[i] - minPrice
        }
    }
    return result
};
Copy the code