首页
Python
Java
前端
数据库
Linux
Chatgpt专题
开发者工具箱
leetcode122专题
leetcode122-Best Time to Buy and Sell Stock II
题目 给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。 在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。 返回 你能获得的 最大 利润 。 示例 1: 输入:prices = [7,1,5,3,6,4] 输出:7 解释:在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股
阅读更多...
代码随想录算法训练营第三十二天|LeetCode122 买卖股票的最佳时机Ⅱ、LeetCode55 跳跃游戏、LeetCode45 跳跃游戏Ⅱ
题1: 指路:122. 买卖股票的最佳时机 II - 力扣(LeetCode) 思路与代码: 基本思路:一天买入一天卖出,得到每部分正利润作为局部最优解,例如prices[7, 1, 5, 3, 6, 4]中,利润分别为[-6, 4, -2, 3, -2],选取每部分正利润为从prices[1]买入prices[2]卖出,再从prices[3]买入prices[4]卖出 。本题无须返回具体买
阅读更多...
代码随想录算法训练营第三十二天| LeetCode122.买卖股票的最佳时机II、LeetCode55.跳跃游戏、LeetCode45.跳跃游戏II
LeetCode 122 买卖股票的最佳时机II 题目链接:122. 买卖股票的最佳时机 II - 力扣(LeetCode) 【解题思路】 利润=当天成交价-昨天成交价 当遇到利润为正数的情况,将其收集。 【解题步骤】 1.定义一个result,将每天的正利润累加 2.循环遍历数组(注意!i从1开始,因为需要从第二天开始减去第一天的价格才能得出利润): 如果当天利润为正数,累加到
阅读更多...
代码随想录 Leetcode122. 买卖股票的最佳时机 II
题目: 代码(首刷自解 2024年2月9日): class Solution {public:int maxProfit(vector<int>& prices) {int res = 0;for (int i = 1; i < prices.size(); ++i) {if (prices[i] - prices[i - 1] > 0) {res += prices[i] - pr
阅读更多...
leetcode122. 买卖股票的最 佳时机 II easy
leetcode122. 买卖股票的最佳时机 II easy 题目描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。 示例 1: 输入: [7,1,5,3,6,4] 输出: 7
阅读更多...
Leetcode122买股票的最佳时机2
代码: class Solution {public int maxProfit(int[] prices) {int n = prices.length;int[][] dp = new int[n][2];dp[0][0] = 0;dp[0][1] = -prices[0];for(int i=1;i<n;i++){dp[i][0] = Math.max(dp[i-1][0],dp[i-1]
阅读更多...