Easy : 122. Best Time to Buy and Sell Stock II
2017-05-09 11:42
41 查看
Best Time to Buy and Sell Stock II
题目:
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 ret = 0; for (int i = 1; i < prices.size(); i++) { ret += max(prices[i] - prices[i - 1], 0); } return ret; } };
解题思路:
本题是很简单的一道求股票最大利润的题,采用贪心算法,尽可能多的买卖股票,并且每次只要有盈利的即卖出,然后再买入;而不是等到盈利最大时卖出;时间复杂度O(n)。
相关文章推荐
- EasyPerformanceCounterHelper
- 【leetcode】 83. Remove Duplicates from Sorted List(easy)
- Eclipse插件-easy-explore-1.0.4
- easy-ui 中的事件触发 (tree)
- VS编辑器主题变换插件-EasyVS
- Easy Climb UVA - 12170 普通dp 离散化 单调队列 滑动窗口
- Android Run Time: An Easy Hack To Make Your Phone Faster And Its Battery Last Longer
- 算法提高 ADV-20 交换Easy
- HDOJ 1040 As Easy As A+B
- easy-chat之好友列表
- EASY_ZJU_PAT_ADVANCED LEVEL 1005
- EasyNVR无插件IPC摄像机直播方案前端构建之:区分页面是自跳转还是分享依据
- EASY_PAT_1065 用Java的BigInteger类解决大数问题
- 【LeetCode】Remove x from (Un)sorted List * 4 - Easy->Medium
- NYOJ593 Take it easy
- 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]
- Curl的curl_easy_perform函数在Debug正常,但在Release崩溃的解决方法
- 基于EasyDarwin框架实现EasyNVR H5无插件直播流媒体服务器方案
- jquery easy ui表单和分页设计
- 高复用率的RTSPClient组件EasyRTSPClient调用说明