The Tribonacci sequence Tn is defined as follows:
Given n, return the value of Tn
Example 1:
/**
* @param {number} n
* @return {number}
*/
var tribonacci = function(n, memo = []) {
if (memo[n]) return memo[n];
if (n === 0) return 0;
if (n === 1 || n === 2) return 1;
const thirdNumber = tribonacci(n - 1, memo) + tribonacci(n - 2, memo) + tribonacci(n - 3, memo)
memo[n] = thirdNumber;
return thirdNumber;
};
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.
Example 1:
Example 2:
/**
* @param {number[]} cost
* @return {number}
*/
var minCostClimbingStairs = function(cost) {
const length = cost.length;
const memo = [];
function dp(cost, index) {
if (index < 0) return 0;
if (index === 0 || index === 1) return cost[index];
if (memo[index]) return memo[index];
memo[index] = cost[index] + Math.min(dp(cost, index - 1), dp(cost, index -2));
return memo[index];
}
return Math.min(dp(cost, length - 1), dp(cost, length - 2));
};
rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.
/**
* @param {number} rowIndex
* @return {number[]}
*/
var getRow = function(rowIndex) {
if (!rowIndex) return [1];
let memo = [1, 1];
let currRow;
while(--rowIndex) {
currRow = [1];
for (let i = 0; i < memo.length - 1; i++) {
currRow.push(memo[i] + memo[i + 1]);
}
currRow.push(1);
memo = currRow;
}
return memo;
};
You are given an array prices where prices[i] is the price of a given stock on the ith day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
Example 1:
Example 2 :
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
const length = prices.length;
let profit = 0;
let buyPrice = Infinity;
let sellPrice;
for (let i = 0; i < length - 1; i++) {
if (prices[i] > buyPrice) continue;
buyPrice = prices[i];
for (let j = i + 1; j < length; j++) {
sellPrice = prices[j];
if (sellPrice <= buyPrice) continue;
if (sellPrice - buyPrice > profit) {
profit = sellPrice - buyPrice;
}
}
}
return profit;
};
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?
Constraints:
n <= 45Example 1:
Example 2:
/**
* @param {number} n
* @return {number}
*/
var climbStairs = function(n) {
let stairs = new Array(n+1).fill(0);
stairs[1] = 1;
stairs[2] = 2;
for (let i = 3; i <= n; i++) {
stairs[i] = stairs[i - 1] + stairs[i - 2];
}
return stairs[n];
};
// 2 ~ 7의 계단 오르는 방법 경우의 수
2 : 1 + 1 = 2
3 : 1 + 2 = 3
4 : 1 + 3 + 1 = 5
5 : 1 + 4 + 3(2+1) = 8
6 : 1 + 5 + 6(3+2+1) + 1 = 13
7 : 1 + 6 + 10(4+3+2+1) + 4 = 21
m * n matrix of ones and zeros, return how many square submatrices have all ones.Example 1
Example 2
arr.length <= 300arr[0].length <= 300arr[i][j] <= 1O(m*n) sol by DP)
| 1 | 1 | 1 |
|---|---|---|
| 1 | 1 | 2 |
| 1 | 2 | 2 |
| 1 | 2 | 3 |
/**
* @param {number[][]} matrix
* @return {number}
*/
var countSquares = function(matrix) {
let result = 0;
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[0].length; j++) {
if (!matrix[i][j]) {
continue;
}
if (i > 0 && j > 0) {
matrix[i][j] = 1 + Math.min(matrix[i - 1][j - 1], matrix[i - 1][j], matrix[i][j - 1]);
}
result += matrix[i][j];
}
}
return result;
};