LeetCode: Best Time to Buy and Sell Stock II [122]
【题目】
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).
【题意】
给定一个数组prices, prices[i]表示第i天的股价。本题没有买卖次数的限制,问最大收益是多少
【思路】
贪心思想。假设股价一路上扬,则持币观望,直到股价升到最高点抛售获利最大。
【代码】
class Solution {
public:
int maxProfit(vector<int> &prices) {
int size=prices.size();
if(size<=1)return 0;
int maxProfit=0;
int buyDate=0;
for(int i=1; i<size; i++){
if(prices[i-1]>prices[i]){
//股价開始下跌,在i-1天抛售
maxProfit+=prices[i-1]-prices[buyDate];
buyDate=i;
}
}
//【注意】别忘了最后一次买卖
maxProfit+=prices[size-1]-prices[buyDate];
return maxProfit;
}
};
LeetCode: Best Time to Buy and Sell Stock II [122]的更多相关文章
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)
先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...
- LeetCode: Best Time to Buy and Sell Stock II 解题报告
Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...
- [LeetCode] 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 al ...
- LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- [leetcode]Best Time to Buy and Sell Stock II @ Python
原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意: Say you have an array ...
- [LeetCode] 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 al ...
- LeetCode——Best Time to Buy and Sell Stock II
Description: Say you have an array for which the ith element is the price of a given stock on day i. ...
- [Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...
随机推荐
- Android-Service组件
转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/40381109 原文:http://developer.android.com ...
- flask+gevent+gunicorn+nginx 初试
1.安装flask pip install flask 2.安装gevent pip install gevent 3.安装gunicorn pip install gunicorn 版本信息例如以下 ...
- 安卓培训第五天---上传文件SD卡
有关如何将文件上传到先说说SD卡: package com.example.lesson05_02; import java.io.File; import java.io.FileNotFoundE ...
- 【转】ubuntu终端方向键不能用(主机名不显示)问题的解决
sudo gedit /etc/passwd 在/etc/passwd中修改该用户对应的shell:/bin/sh改为/bin/bash即可解决该问题 来自:http://blog.csdn.net/ ...
- javascript事件和事件处理
于js期间事件处理被分成三个步骤: 1.发生事件 2.启动事件处理程序 3.事件处理程序做出反应 事件处理程序的调用 1.在javascript中 在javascript中调用事件处理程序,首先要获得 ...
- OSChina 的URL类的源代码重写过程
此代码是 oschina 到手柄形状像 http://www.oschina.net/p/tomcat 这种URL 此类已经废弃,改用 http://www.oschina.net/code/snip ...
- tomcatserver解析(六)-- Acceptor
Acceptor负责用来管理连接到tomcatserver的数量,来看看Acceptor在tomcatserver中的应用,是怎样实现连接管理的,socket连接建立成功之后,是怎样实现内容的读写的( ...
- hdu4570Multi-bit Trie (间隙DP)
Problem Description IP lookup is one of the key functions of routers for packets forwarding and clas ...
- HDU-4628 Pieces 如压力DP
鉴于他的字符串,每一个都能够删除回文子串.子可以是不连续,因此,像更好的模拟压力.求删除整个字符串需要的步骤的最小数量. 最大长度为16,因此不能逐行枚举状态.首先预处理出来全部的的回文子串,然后从第 ...
- Python 提取Twitter转发推文的元素(比方username)
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-24 @author: guaguastd @name: e ...