【leetcode】122.Best Time to Buy and Sell Stock II
2015-07-19 21:23
190 查看
@requires_authorization @author johnsondu @create_time 2015.7.19 21:01 @url [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) /************************ * @description: dynamic programming. * 相邻元素做差,求出所有非负元素的和 * @time_complexity: O(n) * @space_complexity: O(1) ************************/ class Solution { public: int maxProfit(vector<int>& prices) { int p_size = prices.size(); if(p_size < 2) return 0; int ans = 0; for(int i = 1; i < p_size; i ++) { int tmp = prices[i] - prices[i-1]; if(tmp > 0) ans += tmp; } return ans; } };
相关文章推荐
- hdu 3746 (kmp)
- 相关列的基数计算
- 关系数据库基础
- linux常用命令1cd、ls、快捷键、mkdir、rmdir、rm、cp、mv、cat等
- pyenv管理python多版本
- 计蒜之道 初赛 第三场 题解 Manacher o(n)求最长公共回文串 线段树
- 迁移学习( Transfer Learning )---薛贵荣
- POJ 1979 Red and Black
- 自定义标签在IE6-8的困境
- 用WCAT进行IIS压力测试
- 词法分析器总结
- codeforces 555 C Case of Chocolate
- NSRunLoop扩展阅读
- hessian入门
- linux无线网卡驱动安装
- ZOJ 3230 Solving the Problems(数学 优先队列啊)
- C#泛型总结
- 欢迎使用CSDN-markdown编辑器
- 音乐touch ur soul
- 数组矩阵的顺时针输出