第五周:[Leetcode]122. Best Time to Buy and Sell Stock II
来源:互联网 发布:iphone蜂窝数据是什么 编辑:程序博客网 时间:2024/05/29 10:57
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).
贪心算法:只要是隔天获利的,都做这笔交易。
例如:1,2,3,4,5的序列,(2-1)+(3-2)+(4-3)+(5-4)= 5 - 1;
class Solution {public: int maxProfit(vector<int>& prices) { int ans = 0,i,profit; if(prices.size() < 1) return 0; for(i = 0;i < prices.size() - 1;i++){ profit = prices[i+1] - prices[i]; ans += profit > 0 ? profit : 0; } return ans; }};
0 0
- 第五周:[Leetcode]122. Best Time to Buy and Sell Stock II
- LeetCode 122. Best Time to Buy and Sell Stock II
- [Leetcode] 122. Best Time to Buy and Sell Stock II
- [leetcode] 122.Best Time to Buy and Sell Stock II
- 【leetcode】122.Best Time to Buy and Sell Stock II
- 【LeetCode】122.Best Time to Buy and Sell Stock II
- [leetcode] 122. Best Time to Buy and Sell Stock II
- LeetCode #122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II LeetCode
- [LeetCode]122. Best Time to Buy and Sell Stock II
- Leetcode 122. Best Time to Buy and Sell Stock II
- leetcode 122. Best Time to Buy and Sell Stock II
- LeetCode 122. Best Time to Buy and Sell Stock II
- LeetCode *** 122. Best Time to Buy and Sell Stock II
- leetcode-122. Best Time to Buy and Sell Stock II
- leetcode 122. Best Time to Buy and Sell Stock II
- leetcode 122. Best Time to Buy and Sell Stock II
- 【LeetCode】122. Best Time to Buy and Sell Stock II
- git 交互式rebase
- JAVA中的String对象一定不可变?以及String.intern()
- 【Codevs 1088】神经网络 【NOIP 2003】
- 爬虫 自动登陆 抓取 feedback & autosendemail
- 阅读android源码后的一点感想
- 第五周:[Leetcode]122. Best Time to Buy and Sell Stock II
- 三级联动效果
- configure: error: xml2-config not found. Please check your libxml2 installation
- activit与spring的整合
- Ubuntu下的服务器环境安装
- 关于linux的第一次培训
- 第五周:[Leetcode]455. Assign Cookies
- EventBus学习笔记(一)
- 权限与命令间的关系(用户访问文件的基本权限)