[leetcode] 122. Best Time to Buy and Sell Stock II
来源:互联网 发布:360浏览器安装js脚本 编辑:程序博客网 时间:2024/05/29 16:54
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 res =0; for(int i=1; i< prices.size(); ++i){ if(prices[i]-prices[i-1]>0) res += (prices[i] - prices[i-1]); } return res; }};
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
- 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
- LeetCode - 122. Best Time to Buy and Sell Stock II
- 树莓派 Learning 002 装机后的必要操作 --- 04 添加软件源 之 添加公钥 --- 解决“由于没有公钥,无法验证下列签名”问题
- Leetcode:Valid Anagram
- 自己的故事自己讲--富士康之旅
- 论坛推广实战 | 精品帖持续不断引流赚钱?
- 树莓派 Learning 002 装机后的必要操作 --- 05 给树莓派搭建“x86 + pi”环境 -- 安装**32位运行库** -- 解决`E:未发现软件包 xxx` 问题
- [leetcode] 122. Best Time to Buy and Sell Stock II
- 现在特别想写下这些东西。 趁我热情还在。
- 树莓派 Learning 002 装机后的必要操作 --- 00 修改键盘布局
- 数据结构(java)----BinarySearchTree
- Spring整合Quartz框架实现定时任务跑批(Maven完整版)
- Product of Array Except Self
- Ubuntu Linux 使用桂电校园网 上网
- 萌新的Canvas笔记(四)
- WordPress3.0导航菜单图文使用教程