题意:有一个数组,第i个数据代表的是第i天股票的价格,每天只能先卖出再买进(可以不卖出也可以不买进),求最大收益。

思路:自己去弄几个数组比划比划就知道了,比如[1,2,5,3,6],第一天买进,第二天卖出,再买进,第三天卖出,第四天买进,第五天卖出。

真正计算的就是前一天的价格和当天的价格的差值,[1,3,-2,3],大于0买进,否则不买。

代码:

int maxProfit(vector<int>& prices) {
int n = prices.size();
int a[n-];
for(int i = ;i<n-;i++)
a[i] = prices[i+]-prices[i];
int ans = ;
for(int j = ;j<n-;j++)
{
if(a[j]>)
ans += a[j];
}
return ans;
}

leetcode122 Best Time to Buy and Sell Stock的更多相关文章

  1. LeetCode122: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 a ...

  2. Leetcode-122 Best Time to Buy and Sell Stock II

    #122  Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the pric ...

  3. LeetCode122——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 a ...

  4. [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. [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 ...

  8. [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  9. [LintCode] 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 ...

随机推荐

  1. [NOI2003] 文本编辑器 (splay)

    复制炸格式了,就不贴题面了 [NOI2003] 文本编辑器 Solution 对于光标的移动,我们只要记录一下现在在哪里就可以了 Insert操作:手动维护中序遍历结果,即每次取中点像线段树一样一样递 ...

  2. Substrings Sort string 基本操作

    You are given nn strings. Each string consists of lowercase English letters. Rearrange (reorder) the ...

  3. ActiveMQ(4) ActiveMQ JDBC 持久化 Mysql 数据库

    ActiveMQ 消息持久化机制: ActiveMQ 消息的持久化机制有 JDBC.AMQ.KahaDB 和 LevelDB,其中本示例版本(5.15.2)默认机制为 KahaDB.无论哪种持久化机制 ...

  4. Shell之基本用法

    一:shell简介 1.什么是shell shell的中文意思是“外壳”,通俗地讲,shell是一个交互编程接口,通过获得用户输入来驱动操作系统内核完成指定工作.shell除了作为命令解释程序以外,还 ...

  5. 【mysql】索引与排序、重复索引、冗余索引

    索引与排序 排序可能发生2种情况: 1: 对于覆盖索引,直接在索引上查询时,就是有顺序的, using index 2: 先取出数据,形成临时表做filesort(文件排序,但文件可能在磁盘上,也可能 ...

  6. urlparse获取url后面的参数

    copyfrom: http://www.cnpythoner.com/post/263.html 如果给定你一个URL,比如: http://url/api?param=2&param2=4 ...

  7. Objective-C中ORM的运用:实体对象和字典的相互自动转换

    http://blog.csdn.net/cooldragon/article/details/18991973 iOS开发中基于ORM的框架很多,如SQLitePersistentObject,实际 ...

  8. /usr/bin/expect介绍

    /usr/bin/expect介绍 http://blog.csdn.net/zhu_tianwei/article/details/44180637 概述 我们通过Shell可以实现简单的控制流功能 ...

  9. Javascript传参参考

    可参考的细节: <!doctype html> <html lang="en"> <head> <meta charset="U ...

  10. Selenium2+python自动化58-读取Excel数据(xlrd)【转载】

    前言 当登录的账号有多个的时候,我们一般用excel存放测试数据,本节课介绍,python读取excel方法,并保存为字典格式. 一.环境准备 1.先安装xlrd模块,打开cmd,输入pip inst ...