杨乐乐
Never too late

Follow

Never too late

Follow
Day 32 贪心算法 - 买卖股票的最佳时机 ii

Day 32 贪心算法 - 买卖股票的最佳时机 ii

杨乐乐's photo
杨乐乐
·Feb 15, 2023·

1 min read

Table of contents

  • 122. 买卖股票的最佳时机 ii
  • 55. 跳跃游戏
  • 45. 跳跃游戏 ii

122. 买卖股票的最佳时机 ii

function maxProfit(prices: number[]): number {
    let result = 0;

    for (let i = 1; i < prices.length; i++) {
        result += Math.max(0, prices[i] - prices[i - 1]);
    };

    return result;
};

55. 跳跃游戏

function canJump(nums: number[]): boolean {
    let cover = 0;

    if (nums.length === 1) {
        return true;
    }

    for (let i = 0; i <= cover; i++) {
        cover = Math.max(i + nums[i], cover);
        if (cover >= nums.length - 1) {
            return true;
        }
    }

    return false;
};

45. 跳跃游戏 ii

function jump(nums: number[]): number {
    let steps = 0;
    let curStepsMaxReach = 0;
    let oneMoreStepsMaxReach = nums[0];

    for (let i = 1; i < nums.length; i++) {
        if (i > curStepsMaxReach) {
            steps++;
            curStepsMaxReach = oneMoreStepsMaxReach;
        }
        oneMoreStepsMaxReach = Math.max(oneMoreStepsMaxReach, i + nums[i]);
    }

    return steps;
};
 
Share this