首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
买卖股票的最好时机 iii
[编程题]买卖股票的最好时机 iii
热度指数:18852
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32M,其他语言64M
算法知识视频讲解
假设你有一个数组,其中第i个元素是某只股票在第i天的价格。
设计一个算法来求最大的利润。你最多可以进行两次交易。
注意:
你不能同时进行多个交易(即,你必须在再次购买之前出售之前买的股票)。
示例1
输入
[1,4,2]
输出
3
示例2
输入
[2,4,1]
输出
2
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(18)
邀请回答
收藏(183)
分享
提交结果有问题?
66个回答
1篇题解
开通博客
华科不平凡
发表于 2020-09-24 14:14:36
利用两个dp数组 dp1[i]表示以第i个元素结尾的子串的最大收益 dp2[i]表示以第i个元素开始的子串的最大收益 代码如下: // // Created by jt on 2020/9/24. // #include <vector> using namespace std;
展开全文
问题信息
数组
难度:
66条回答
183收藏
30394浏览
热门推荐
通过挑战的用户
查看代码
阿阿
2022-09-14 14:26:44
牛客38441...
2022-09-13 20:32:06
牛客58603...
2022-09-09 20:46:01
说我这儿没问题...
2022-09-08 12:02:09
牛客68124...
2022-09-03 20:41:13
相关试题
() 通过计算机网络给 () 发送...
网络基础
评论
(1)
无源晶振起振电容容量选择方法
元器件
评论
(1)
如果让你策划设计一个影片评论功能的...
竞品研究
评论
(1)
手写代码:循环链表插入元素
评论
(1)
计算二元分类的Jaccard指数
机器学习
评论
(1)
买卖股票的最好时机 iii
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class Solution { /** * * @param prices int整型一维数组 * @return int整型 */ public int maxProfit (int[] prices) { // write code here } }
class Solution { public: /** * * @param prices int整型vector * @return int整型 */ int maxProfit(vector
& prices) { // write code here } };
# # # @param prices int整型一维数组 # @return int整型 # class Solution: def maxProfit(self , prices ): # write code here
/** * * @param prices int整型一维数组 * @return int整型 */ function maxProfit( prices ) { // write code here } module.exports = { maxProfit : maxProfit };
# # # @param prices int整型一维数组 # @return int整型 # class Solution: def maxProfit(self , prices ): # write code here
package main /** * * @param prices int整型一维数组 * @return int整型 */ func maxProfit( prices []int ) int { // write code here }
[1,4,2]
3
[2,4,1]
2