Leetcode_122_Best Time to Buy and Sell Stock II
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43155725
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).
思路:
(1)题意为给定一个数组,数组中第i个元素的值对应着第i天的股票,你可以完成多次交易,但是每次交易只能买入一次并卖出,求进行多次交易所能得到的最大利润。该题为Best Time to Buy and Sell Stock的加强版。
(2)与Best Time to Buy and Sell Stock类似,该题同样考查的是最大差值。只不过该题考查数组中所有相邻且递增元素的数值之差的总和。只要第i+1天的值大于第i天的值,则可买入,求得利润(差值),遍历整个数组,得到所用差值之和即为总的利润。
(3)该题还是比较简单。希望对你有所帮助。
算法代码实现如下:
/** * * @author liqq */ public int maxProfit(int[] x) { if (x == null || x.length <= 1) return 0; int min = x[0]; int profit = 0; for (int i = 0; i < x.length; i++) { if (min > x[i]) { min = x[i]; } else { profit += x[i] - min; min = x[i]; } } return profit; }
Leetcode_122_Best Time to Buy and Sell Stock II的更多相关文章
- [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 ...
- 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 ...
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- 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 ...
- 【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 ...
- 31. 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 price ...
- 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 ...
- 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 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
随机推荐
- android 获取栈顶activty的方法总结(兼容API 5.0)
声明:本文为Dujinyang CSDN原创投稿文章,未经许可,禁止任何形式的转载. 最近5.0\6.0\7.0 安卓系统都陆续上岗了,兼容性和代码更新是个很头疼的问题,这次我们来说下TASK的基础和 ...
- NLP系列(4)_朴素贝叶斯实战与进阶
作者: 寒小阳 && 龙心尘 时间:2016年2月. 出处:http://blog.csdn.net/han_xiaoyang/article/details/50629608 htt ...
- DrawerLayout案例
布局文件: <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget ...
- log file sync 等侍值高的一般通用解决办法
log file sync等待时间发生在redo log从log buffer写入到log file期间. 下面对log file sync做个详细的解释. 何时发生日志写入: 1.commit或者r ...
- argparse库 学习记录
初始化 始见参数 name or flags action nargs default type choices required help dest metavar 总结 继上次的optparser ...
- 有无序的实数列V[N],要求求里面大小相邻的实数的差的最大值,关键是要求线性空间和线性时间。
int findMaxDifBt2Nums(int* arr, int len) { int maxItem = arr[0], minItem = arr[0]; for (int i = 1; i ...
- Android中GridView的一些特殊属性
GridView的一些特殊属性: 1.android:numColumns="auto_fit" //GridView的列数设置为自动 2.android:columnWidt ...
- Android Studio 使用wifi调试插件
由于手机亦或是数据线的问题,在应用开发过程中会时不时地遇到手机突然连不上电脑的尴尬时刻,于是就学习了如何使用wifi进行应用调试.下面就具体介绍一下adb wifi插件的安装和使用.其实我们只需要安装 ...
- javascript之内置函数
1.常规函数 (1)alert函数:显示一个警告对话框,包括一个OK按钮. (2)confirm函数:显示一个确认对话框,包括OK.Cancel按钮. (3)escape函数:将字符转换成Unicod ...
- 无网络环境下安装Dynamics CRM
在安装CRM时会需要很多的组件支持,没有这些组件是没法安装的,一般我们都是选择机器联网后在线安装,但也有特殊情况确实不能联网的,可参考这篇文章 https://blogs.msdn.microsoft ...