【leetcode】Best Time to Buy and Sell Stock II
2015-11-19 20:35
92 查看
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).
Personal Note:
实际上就是求所有上升区段的幅度和。
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 profit = 0; if (prices.size() < 2) return result; for (vector<int>::iterator it = prices.begin() + 1; it != prices.end(); ++it) { profit = *it - *(it-1); if (profit > 0) result += profit; } return result; } };
Personal Note:
实际上就是求所有上升区段的幅度和。
相关文章推荐
- ssh命令找不到解决访问
- netbeans设置语言
- spark1.4加载mysql数据 创建Dataframe及join操作连接方法问题
- AC自动机
- netbeans设置语言
- windows10下配置ffmpeg
- 数据库连接配置1 -- Tomcat 中的 JDBC Pool
- ”南大软院大神养成计划“_第四天的学习
- hdoj 1227 Fast Food 【dp】
- Android线性布局权重Weight效果展示
- Nodejs Express 开发必备知识点
- 【iOS错误笔记】'CADbugPrintf.h'file not found
- 阻塞队列
- Linux kdb命令
- 二十道前端面试题
- find 与grep命令
- 字节流与字符流的区别详解
- Android项目-Widget
- Android进阶-点击事件的响应问题
- Learning ROS for Robotics Programming Second Edition学习笔记(三) indigo rplidar rviz slam