122.Best Time to Buy and Sell Stock II---dp
题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/
题目大意:基本定义与121类似,不同点:121买卖股票只能有一次,且在这所有的一次买卖中找出最大利润值;122买卖股票不限次数,要求在这所有次数中的最大利润值(这里题目描述不准确,其实不是求某一次买卖得到的最大利润值,而是求所有次买卖所获得的总利润值)。
题解(借鉴):这里不用利用121的贪心思想,直接计算每一天中,只要比前一天贵,就卖出当前股票获取利润。(但是我觉得这里题目漏洞太大了,也就是说1,2,3这组数据,得到的最大值是2,由(2-1)+(3-2)得到)。代码如下(耗时1ms):
public int maxProfit(int[] prices) {
int profit = 0;
for(int i = 1;i < prices.length; i++) {
if(prices[i] > prices[i - 1]) {
profit += prices[i] - prices[i - 1];
}
}
return profit;
}
122.Best Time to Buy and Sell Stock II---dp的更多相关文章
- 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 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 122. Best Time to Buy and Sell Stock II@python
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode:122. Best Time to Buy and Sell Stock II(java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...
- 【刷题-LeetCode】122 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 ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- !!!!!122. 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 122. Best Time to Buy and Sell Stock II (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode 122. Best Time to Buy and Sell Stock II ----- java
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- BZOJ 2427 软件安装(强连通分量+树形背包)
题意:现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大).但是现在有 ...
- CodeForces 860D Wizard's Tour
题意 给出一张无向图,要求找出尽量多的长度为2的不同路径(边不可以重复使用,点可以重复使用) 分析 yzy:这是原题 http://www.lydsy.com/JudgeOnline/problem. ...
- P1939 【模板】矩阵加速(数列)
题目描述 a[1]=a[2]=a[3]=1 a[x]=a[x-3]+a[x-1] (x>3) 求a数列的第n项对1000000007(10^9+7)取余的值. 输入输出格式 输入格式: 第一行一 ...
- [TJOI2013]单词 AC自动机
题面: 洛谷 题解: 很久之前做的题了,只不过之前一直90....最近才发现是哪里写错了. 我们对字符集建AC自动机. 首先考虑一个暴力的做法,把文章当做一个长串,直接在自动机上跳,但是我们会发现,这 ...
- POJ.1426 Find The Multiple (BFS)
POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...
- 【agc017E】Jigsaw
Portal -->agc017 Description 给你\(n\)块积木,每块积木由三个矩形组成,中间的矩形最高高度为\(h\),左边的矩形高度为\(a_i\)离底边高度为\(c_i\), ...
- VSAM:视频监控系统 A System for Video Surveillance and Monitoring
VSAM(VideoSurveillance and Monitoring)视频监控系统 Robotics Institute CMU 1:引言 2:试验床介绍 3:基本的视频分析算法:运动目标检测, ...
- 数据分析与展示---Numpy入门
概括: 一:数据维度 (一)一维数据 (二)二维数据 (三)多维数据 (四)高维数据 二:Numpy的数组对象:ndarray (一)Numpy介绍 (二)N维数组对象ndarray (三)ndarr ...
- VS Code 配置删除左边单词快捷键(同Sublime 和 Atom)
VS Code 中删除一行的快捷键默认是 cmd + shift + k,或者使用简介 cmd + x,对于一个长期使用 Sublime 和 Atom 的程序猿来说,在VS Code 上删除行,特别是 ...
- Codechef Dynamic Trees and Queries
Home » Practice(Hard) » Dynamic Trees and Queries Problem Code: ANUDTQSubmit https://www.codechef.co ...