`

Best Time to Buy and Sell Stock I

 
阅读更多

question:

 

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

 

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

 

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int n = prices.size();
        if(n <= 1) return 0;
        
        int maxAll = 0;
        int maxLast = 0;
        
        int maxJump = 0;
        int mjb = prices[0]; // max jump begin element
        
        vector<int>::iterator it = prices.begin();
        vector<int>::iterator li = it;
        it++;
        for(;it != prices.end(); it++, li++) {
            if(*it >= *li) {
                maxLast += *it - *li;
                maxJump += *it - *li;
            } else {
                maxLast = 0; 
                if(*it < mjb) {
                    mjb = *it;
                    maxJump = 0;
                } else {
                    maxJump += *it - *li;
                }
            }
            maxAll = max(maxAll, maxLast);
            maxAll = max(maxAll, maxJump);
        }
        return maxAll;
    }
    
};

 

0
4
分享到:
评论

相关推荐

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

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

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

    - 如果当前价格`prices[i]`小于`buy_price`,则更新`buy_price`为`prices[i]`。 - 否则,计算`profit = prices[i] - buy_price`,并将`max_profit`更新为`max(max_profit, profit)`。 3. 返回`max_profit`作为最终...

    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 II.java

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

    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"是一个典型的数据结构和算法问题,通常出现在编程面试和算法训练中。该问题的目标是在给定的股票价格数组中找出最大的利润,但是与简单买卖一次...

    javalruleetcode-leetcode-java:力码笔记

    121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and Sell Stock III 141.Linked List Cycle 142.Linked List Cycle II 188.Best Time to Buy and Sell Stock IV...

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

    python python_leetcode题解之123_Best_Time_to_Buy_and_Sell_Stock_III

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

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

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

    - Best Time to Buy and Sell Stock IV:与前三者不同,可能需要多次买入和卖出。 - Best Time to Buy and Sell Stock with Cooldown:买卖后需要冷却一天。 - Best Time to Buy and Sell Stock with Transaction...

    python-leetcode题解121-Best-Time-to-Buy-and-Sell-Stock

    在探讨如何用Python解决LeetCode上的第121题“最佳买卖股票时机”之前,我们有必要对相关概念和解决策略进行充分了解。本题要求找到只进行一次买卖的最大利润,是动态规划算法中的典型问题。 首先,我们要明确题目...

    股票收益leetcode-leetcode:leetcode摘要

    股票收益leetcode LeetCode 股票问题 Best Time to Buy and Sell Stock ...一次交易,找最大收益 ...i ...i) ...i-low) ...i-buy1) #找第一次交易最大收益 buy2 = min(buy2, i-prof1) #找用第一次收益购买的股票仍

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

    在JavaScript的世界里,LeetCode是一个广受欢迎的在线编程平台,它提供了一系列的算法题目,帮助程序员提升编程技能。其中“121. 买卖股票的最佳时机”是一个经典的动态规划问题,经常作为算法入门的练习题。...

    js-leetcode题解之122-best-time-to-buy-and-sell-stock-ii.js

    在JavaScript(简称JS)编程中,LeetCode题库提供了一系列的编程挑战,旨在帮助开发者提升算法和编程能力。本篇文档将深入分析LeetCode 122号题目,即“买卖股票的最佳时机 II”,并给出一种JS语言的解决方案。...

    Andy619-Zhu#CS-Notes-chu#63. 股票的最大利润1

    63. 股票的最大利润题目链接Leetcode:121. Best Time to Buy and Sell Stock题目描述可以有一次买入和一次卖出,买入必

    leetcode分类-LeetCode:力码

    leetcode 分类 LeetCode LeetCode Java Solution 2018年5月31日 更新 最近刷了一遍 ...题,把相似的题目都放在了一起,比如Best Time to Buy and Sell Stock的6道题,这些题目放在一起刷效果更好。 简书博客:

    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 ...

Global site tag (gtag.js) - Google Analytics