// Best Time to Buy and Sell Stock I class Solution { public: int maxProfit(vector<int> &prices) { if(prices.size() < 2) return 0; int mj = 0, mjIdx = 0; int ret = 0; for(int i = 1; i < prices.size(); ++i) { if(prices[i] >= prices[i-1]) { mj = prices[i] - prices[mjIdx]; } else { if(prices[i] < prices[mjIdx]) { mjIdx = i; } } ret = max(ret, mj); } return ret; } }; // Best Time to Buy and Sell Stock II class Solution { public: int maxProfit(vector<int> &prices) { if(prices.size() < 2) return 0; int ms = 0; int ret = 0; for(int i = 1; i < prices.size(); ++i) { if(prices[i] >= prices[i-1]) { ms += prices[i] - prices[i-1]; } else { ret += ms; ms = 0; } } ret += ms; return ret; } }; // Best Time to Buy and Sell Stock III class Solution { public: int maxProfit(vector<int> &prices) { if(prices.size() < 2) return 0; int n = prices.size(); vector<int> l(n); vector<int> r(n); int minv = prices[0]; l[0] = 0; for(int i = 1; i < n; ++i) { minv = min(minv, prices[i]); l[i] = max(l[i-1], prices[i] - minv); } int maxv = prices[n-1]; r[n-1] = 0; for(int i = n-2; i >= 0; --i) { maxv = max(maxv, prices[i]); r[i] = max(r[i+1], maxv - prices[i]); } int ret = 0; for(int i = 0; i < n; ++i) { ret = max(ret, l[i] + r[i]); } return ret; } };
欢迎关注微信公众号——计算机视觉
相关推荐
leetcode题目:Best Time to Buy and Sell Stock II
java lru leetcode leetcode-java leetcode刷题笔记 ...III 141.Linked List Cycle 142.Linked List Cycle II 188.Best Time to Buy and Sell Stock IV 217.Contains Duplicate 263.Ugly Number 264.Ugly Number II
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 ...
股票收益leetcode LeetCode 股票问题 Best Time to Buy and Sell Stock ...一次交易,找最大收益 ...i ...i) ...i-low) ...i-buy1) #找第一次交易最大收益 buy2 = min(buy2, i-prof1) #找用第一次收益购买的股票仍
Best Time to Buy and Sell Stock III Best Time to Buy and Sell Stock IV Best Time to Buy and Sell Stock with Cooldown Interleaving String Scramble String Minimum Path Sum Edit Distance Decode Ways ...
63. 股票的最大利润题目链接Leetcode:121. Best Time to Buy and Sell Stock题目描述可以有一次买入和一次卖出,买入必
It is written to help C++ developers of all skill levels and shows by example how to write understandable, flexible, maintainable, and efficient C++ code. Even if you are a seasoned C++ developer, ...
Sustainable Software Development Patterns and Best Practices with C++ 17
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 ...
I see many of those students now moving to C++ in their school work or career. The C++ language is becoming an industry-accepted standard programming language, using the solid foundation of C to gain ...
Throughout my career, I have mentored both students and fellow employees in programming, and many of them have suggested that I write my thoughts down in book form. However, I have typically responded...
My goal has been to present, in a way that allows novice C++ programmers to learn them quickly, what I consider to be the most important strategies and tactics I have learned from others in the eight...
C++ Coding Standards: 101 Rules, Guidelines, and Best Practices,英文版。C++ In-Depth系列丛书之一,C++程序员必看。
correct, effective lambda expressions How std::atomic differs from volatile, how each should be used, and how they relate to C++'s concurrency API How best practices in "old" C++ programming (i.e., ...
C++ solutions for the leetcode 这是我自己写的leetcode的题解,目前已经完成了70%左右,后续部分会很快更新 这里放上来的代码都能保证是最优解(复杂度最优) 当然有些题目因为测试数据等原因可能会和统计中最快的...
Malik's time-tested, student-centered methodology uses a strong focus on problem-solving and full-code examples to vividly demonstrate the how and why of applying programming concepts and utilizing ...
A practical guide to developing Windows Store apps with C++ and XAML Overview Details the most important features of C++, XAML, and WinRT for building fantastic Windows Store apps Full of detailed ...
The best-selling C++ How to Program is accessible to readers with little or no programming experience, yet comprehensive enough for the professional programmer. The Deitels’ signature live-code ...
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 ...