Best Time to Buy and Sell Stock II(leetcode)
来源:互联网 发布:霍华德站立摸高数据 编辑:程序博客网 时间:2024/06/05 05:52
Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock II
- 题目
- 解决
题目
leetcode题目
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) { int result = 0; int num = prices.size(); for (int i = 1; i < num; i++) { result += (prices[i] - prices[i - 1] > 0) ? prices[i] - prices[i - 1] : 0; } return result; }};
阅读全文
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
- 记录:non-compatible bean definition of same name and class [com.XXX.XXX]
- Java通过python命令执行DataX任务
- WINDOW与LINUX的TCP/IP通信
- Git,GitHub入门
- laytpl--前端数据绑定
- Best Time to Buy and Sell Stock II(leetcode)
- URI和URL的区别
- Eclipse的菜单简介
- MySQL学习
- sql 学习心得 第一篇
- ruby环境搭建记录
- 南丁格尔玫瑰图-修改扇形块的颜色与玫瑰图添加图片与文字
- python与R画柱形图
- 创建新用户,grant授权,角色,建表