`

Best Time to Buy and Sell Stock II

阅读更多
Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

与第一个股票问题的区别是这里我们可以进行多次交易,我们同样用两个变量,profit用来记录当前交易是否盈利,如果是盈利就加入到总利润max中,如果亏损就继续往下查找。这样只要能盈利的都会记录到max中,代码如下:
public class Solution {
    public int maxProfit(int[] prices) {
        int profit = 0;
        int max = 0;
        for(int i = 1; i < prices.length; i++) {
            profit = prices[i] - prices[i - 1];
            if(profit > 0) 
                max += profit;
        }
        return max;
    }
}
分享到:
评论

相关推荐

    leetcode题目:Best Time to Buy and Sell Stock II

    《最佳买卖股票时机II》是LeetCode中的一道经典编程题目,主要涉及动态规划和数组操作的知识点。在这个问题中,我们被赋予一个整数数组`prices`,表示某支股票每天的价格。任务是找到在允许进行多次交易的情况下,...

    java-leetcode题解之Best Time to Buy and Sell Stock II.java

    - 题目理解:Best Time to Buy and Sell Stock II这道题目的目的是寻找一个最佳的股票买卖时机,使得获得的利润最大化。 - 问题分析:此题是一道典型的动态规划问题,也可以使用贪心算法解决。 - 解题思路:贪心算法...

    java-leetcode题解之Best Time to Buy and Sell Stock III.java

    在深入探讨“java-leetcode题解之Best Time to Buy and Sell Stock III.java”这一文件内容之前,我们需要了解LeetCode平台及其题解的价值。LeetCode是一个专门面向技术面试的在线编程平台,它提供了一系列的编程...

    java-leetcode题解之Best Time to Buy and Sell Stock IV.java

    本篇Java题解关注的是“Best Time to Buy and Sell Stock IV”(买卖股票的最佳时机 IV),这是该系列问题中的一个变体,涉及动态规划这一算法思想。 首先,需要明确问题的具体要求。在这个变体中,给定一个数组,...

    java-leetcode题解之Best Time to Buy and Sell Stock with Cooldown

    在解决股票交易问题时,程序员们经常会遇到“Best Time to Buy and Sell Stock with Cooldown”这类问题。该问题属于动态规划的经典案例,通常被列为中等难度。在这类问题中,给定一个整数数组表示某只股票的价格,...

    java-leetcode题解之Best Time to Buy and Sell Stock I.java

    Best Time to Buy and Sell Stock I是LeetCode网站上的一道著名的编程题目,它属于动态规划的范畴。这道题目通常被称为“买卖股票的最佳时机”,主要是考察程序员对动态规划思想的理解和应用。在这个问题中,给定一...

    python-leetcode题解之122-Best-Time-to-Buy-and-Sell-Stock-II

    在探讨python实现leetcode第122题"Best Time to Buy and Sell Stock II"的解决方案前,我们首先需要明确题目要求。第122题属于动态规划和贪心算法范畴,要求编写一个程序,通过分析给定的股票价格数组来找出最大的...

    javalruleetcode-leetcode-java:力码笔记

    java lru leetcode leetcode-java leetcode刷题笔记 已做题目列表 1.Two Sum 3.Longest Substring ...121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time ...II

    3、动态规划必练题(含解法).pdf

    - Best Time to Buy and Sell Stock II:不限制交易次数,寻找最大利润。 - Best Time to Buy and Sell Stock III:存在多日限制交易,最多两次交易。 - Best Time to Buy and Sell Stock IV:与前三者不同,可能...

    股票收益leetcode-leetcode:leetcode摘要

    II (Easy) 可多次交易,找总最大收益 for i in range(1,len(prices): profit+=prices[i]-prices[i-1] if prices[i]-prices[i-1]&gt;0 Best Time to Buy and Sell Stock III (Hard) 最多两次交易(同理固定次数的多次)...

    lrucacheleetcode-LeetCode:CppSourceCode的LeetCode解决方案

    Best Time to Buy and Sell Stock 122 买卖股票的最佳时机 Ⅱ Best Time to Buy and Sell StockⅡ 123 买卖股票的最佳时机 Ⅲ Best Time to Buy and Sell StockⅢ 188 买卖股票的最佳时机Ⅳ Best Time to Buy and ...

    js-leetcode题解之123-best-time-to-buy-and-sell-stock-iii.js

    在JavaScript中,LeetCode题目"Best Time to Buy and Sell Stock III"是一个典型的数据结构和算法问题,通常出现在编程面试和算法训练中。该问题的目标是在给定的股票价格数组中找出最大的利润,但是与简单买卖一次...

    leetcode-常见考题4.pdf

    第二个问题“Best Time to Buy and Sell Stock II”(122题),题目条件相同,但允许进行多笔交易。在一次交易中,买入后再卖出才算是完成了一笔交易,且不能同时买入多股股票。此问题可以用贪心算法解决,即遍历...

    python-leetcode题解之123-Best-Time-to-Buy-and-Sell-Stock-III

    python python_leetcode题解之123_Best_Time_to_Buy_and_Sell_Stock_III

    oj题.zip

    7. **122.py** - 可能是LeetCode的122题,"Best Time to Buy and Sell Stock II"(买卖股票的最佳时机II),这是一道关于动态规划或贪心策略的题目,用于找出股票交易的最佳策略。 8. **156.py** - 这可能是...

    算法面试通关40讲完整课件 25-26 贪心算法

    - **股票买卖问题**:如LeetCode上的题目《Best Time to Buy and Sell Stock II》,通过贪心策略,每次遇到股价低于之前最低价时买入,遇到高于当前价格时卖出,可以实现利润的最大化。 - **零钱找零问题**:...

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate Reverse Polish Notation #169 Majority Element #171 Excel Sheet Column Number #217 Contains Duplicate #226 Invert Binary Tree #237 ...

    lrucacheleetcode-LeetCode_30Day:力扣30天挑战赛

    Best Time to Buy and Sell Stock II 6 GROUP ANAGRAMS 7 COUNTING ELEMENTS 日 问题描述 问题和解决方案链接 Git 解决方案页面 8 Middle of the Linked List 9 Backspace String Compare 10 Min Stack 11 Diameter ...

    leetcode代码200题c++

    3. **Best Time to Buy and Sell Stock II**:这是一个经典的动态规划问题,用于找出股票的最佳买卖时机,可以连续多次买卖。理解状态转移方程和动态规划的原理至关重要。 4. **Jump Game II**:此问题涉及到广度...

Global site tag (gtag.js) - Google Analytics