leetcode Best Time to Buy and Sell Stock II
来源:互联网 发布:java初级题目 编辑:程序博客网 时间:2024/06/05 07:37
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).
思路,贪心,每次找到比前一个大的就记录
class Solution {public: int maxProfit(vector<int>& prices) { if (prices.empty()) return 0; int min = prices[0],profit = 0;bool flag = true; int len = prices.size(); for (int i =1 ;i!= len;++i) { if (prices[i] > prices[i -1 ]){profit += prices[i] - min; }min = prices[i]; } return profit; }};
0 0
- [LeetCode]Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock II
- Leetcode: Best Time to Buy and Sell Stock II
- [Leetcode] Best Time to Buy and Sell Stock II
- LeetCode : Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II
- leetcode 61: Best Time to Buy and Sell Stock II
- [LeetCode] Best time to buy and sell a stock II
- 【leetcode】Best Time to Buy and Sell Stock II
- LeetCode - Best Time to Buy and Sell Stock II
- LeetCode之Best Time to Buy and Sell Stock II
- [LeetCode]Best Time to Buy and Sell Stock II
- [Leetcode]Best Time to Buy and Sell Stock II
- [leetcode]Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II
- LeetCode-Best Time to Buy and Sell Stock II
- [leetcode] Best Time to Buy and Sell Stock II
- 数据可视化与D3.js
- C++刷题——1910: 字符统计
- strange function
- chencang-----为爬虫设置User-Agent
- chencang-----是使用淘宝API获取数据好,还是利用爬虫去抓取数据好呢
- leetcode Best Time to Buy and Sell Stock II
- 第9周项目4-我的向量类
- JavaScript特效实例013-日期选择器
- Windows下amoeba安装及读写分离配置
- chencang-----labin编译错误解决办法
- NWPU省赛热身赛——D,入度和出度
- 2015年第十一周项目二:职员有薪水了(2)
- 高版本chrome不再支持window.showmodaldialog 的临时替换方案【用window.open】
- java 设计模式之状态模式