LeetCode-Best Time to Buy and Sell Stock II
来源:互联网 发布:企业目录搜索软件 编辑:程序博客网 时间:2024/06/01 11:12
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).
Solution:Code:
<span style="font-size:14px;">class Solution {public: int maxProfit(vector<int> &prices) { const int length = prices.size(); if (length < 2) return 0; int lowest = prices[0]; int profit = 0; for (int i = 1; i < length; ++i) if (prices[i] < prices[i-1]) { profit += prices[i-1]-lowest; lowest = prices[i]; } profit += prices[length-1]-lowest; return profit; }};</span>
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
- 六款值得推荐的android(安卓)开源框架简介
- 排序算法(3)-堆排序
- 用valgrind 检测内存错误
- CMD命令Tasklist、Taskkill与筛选器
- python命令行参数处理:argparse、optparse和getopt
- LeetCode-Best Time to Buy and Sell Stock II
- android Handler 详解
- Mongodb 部署一个分片集群
- 黑马程序员——String类中常用方法总结
- PL/SQL 远程连接数据库
- QT+OpenGL学习之路
- 通过节点的层次关系获取节点对象(父节点,子节点,兄弟节点)
- socket
- JAVA 获取字符串中每一个字母出现的次数 TreeMap 解