leetcode Best_Time_to Buy and Sell_Stock_II
来源:互联网 发布:郑州财经网络管理中心 编辑:程序博客网 时间:2024/06/07 05:47
/** * 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). **/#include <iostream>#include <vector>using namespace std;class Solution{public: int maxProfit(vector<int>& prices){ if(prices.size()==0) return 0; int ans=0; for(auto i=prices.begin()+1;i!=prices.end();++i){ if(*i>*(i-1)) //遍历价格数组低买高卖 ans+=*(i)-*(i-1); } return ans; }};int main(){ vector<int> prices = {4, 3, 4, 5, 7, 8, 9, 10, 4, 6, 3}; Solution s; cout << s.maxProfit(prices) << endl;}
0 0
- leetcode Best_Time_to Buy and Sell_Stock_II
- leetcode Best Time to Buy and Sell
- [LeetCode] [LeetCode]Best Time to Buy and Sell Stock
- 【LeetCode】Best Time to Buy and Sell Stock & Best Time to Buy and Sell Stock
- 【LeetCode with Python】 Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock
- LeetCode Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock III
- Leetcode: Best time to buy and sell a stock I
- Leetcode: Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock III
- [Leetcode] Best Time to Buy and Sell Stock
- [Leetcode] Best Time to Buy and Sell Stock II
- [Leetcode] Best Time to Buy and Sell Stock III
- LeetCode : Best Time to Buy and Sell Stock II
- [leetcode] Best Time to Buy and Sell Stock
- 0008、node 之登录状态的保存和退出
- 加速我们的maven下载速度
- 第一次写Blog QwQ
- LOL-国外的灵魂画师
- Android Studio 简单生成so文件并调用
- leetcode Best_Time_to Buy and Sell_Stock_II
- @DatetimeFormat与@JsonFormat注解
- iTerm2 恢复默认设置
- maven解释、安装、添加依赖
- 尝试在win7x64系统上查看tex格式的文档
- Android Volley完全解析(一),初识Volley的基本用法
- 若干秒后不操作自动关闭窗口
- 汇编语言——寻址方式的综合应用及转移指令的原理
- 复盘二进制的习题(2)