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 ...
随机推荐
- net搭建热插拔式web框架(沙箱的构建)
net搭建热插拔式web框架(沙箱的构建) 上周五写了一个实现原理篇,在评论中看到有朋友也遇到了我的问题,真的是有种他乡遇知己的感觉,整个系列我一定会坚持写完,并在最后把代码开源到git中.上一篇文章 ...
- .NET 并行(多核)编程系列之五 Task执行和异常处理
原文:.NET 并行(多核)编程系列之五 Task执行和异常处理 .NET 并行(多核)编程系列之五 Task执行和异常处理 前言:本篇主要讲述等待task执行完成. 本篇的议题如下: 1. 等待Ta ...
- 七牛对用户使用webp图片格式的使用建议
Qiniu 七牛问题解答 Chrome浏览器是可打开WebP格式的.可是并非全部的浏览器都支持webp格式,比如360.ie等浏览器是不支持的. WebP格式,谷歌(google)开发的一种旨在加快图 ...
- Java多线程<1>
1.Java多线程的概念: 线(Thread):它指的是一个任务的从开始执行流程到结束. 穿线提供执行任务的机构.供java条款.在一个程序可以启动多个并发线程.候执行. 在单处理器系统中,多个线程共 ...
- HDU 1541 Stars (树状数组)
Problem Description Astronomers often examine star maps where stars are represented by points on a p ...
- 跑Java -jar somefile.jar时会发生什么(一个)
最近阅读JVM源代码.一些想法写Blog分享.于是,他开了这么一个新课题. 第一篇文章取名字的时候让我很困惑,我代码的阅读是从Launcher開始入手的,也就是Java.exe(假设是windows平 ...
- 10624 - Super Number
题目链接 题意:给出n到m的范围,求出一个数在前i位数组成的数字能被i整除.假设存在输出这个数,假设不存在.输出-1. 思路:回溯,每次放第i位,然后推断是否符合题意.这题踩着时间过去的2.6s(看了 ...
- linux 使用外部设备的(光盘) 安装和更新库
1. 安装光盘(文件夹不存在,创建) mount -t auto /dev/cdrom /mnt/cdrom 2. 更改索引文件,指定读取本地文件 vi /etc/yum.repos.d/CentOS ...
- jquery animate的递归调用
去年面试过一家做网站的 公司,看了一下他们的案例,看懂一个蛮有意思的 ,一个房地产的官网,是用flash做的.感觉不错. 于是,闲暇之际,简单的jquery 模仿做了一下.下面是我的效果图.脚本也没啥 ...
- struts2-dojo-plugin-2.3.1.2.jar!/struts-plugin.xml:29:119
Unable to load configuration. - bean - jar:file:/D:/code_workspace/SSHWorkSpace3/.metadata/.plugins/ ...