Best Time to Buy and Sell Stock II
来源:互联网 发布:刷蓝钻成长值软件 编辑:程序博客网 时间:2024/05/18 01:07
这是道非常简单的题目,但是刚开始的时候我实在是想的太多了,基本的思想很简单,就是找到所有的上升子序列,将头尾的差值加起来就可以了。不过我最开始一直在考虑如何快速方便地找到上升子序列,结果将简单的问题复杂化。
所以这个问题就是,相邻两个数做差,后面的数大的话,这个差就是有效的。
所以这个问题就是,相邻两个数做差,后面的数大的话,这个差就是有效的。
class Solution {public: int maxProfit(vector<int> &prices) { int res = 0; for (int i = 1; i < prices.size(); ++i) { int delta = prices[i] - prices[i - 1]; if (delta > 0) res += delta; } return res; }};http://oj.leetcode.com/problems/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
- 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
- Best Time to Buy and Sell Stock II
- 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
- Best Time to Buy and Sell Stock II
- 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
- Java对象占用内存大小
- linux日志---命令
- C语言字符串操作函数
- 零线和地线的区别,示波器如何测量市电?
- 手机卫士12-号码归属地查询
- Best Time to Buy and Sell Stock II
- 网络协议之四(ICMP)
- WCF服务编程-契约回调(转载)
- linux日志--其他
- iOS多线程编程之NSThread的使用
- 学习笔记(一):计算机体系架构的术语和基本概念
- 服务器集群负载均衡(F5,LVS,DNS,CDN)区别以及选型
- 行为型AI:寻找迷宫出口
- 转换为二进制数注意点