博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 714. 买卖股票的最佳时机含手续费(DAY 27) ---- 动态规划学习期
阅读量:1993 次
发布时间:2019-04-27

本文共 1017 字,大约阅读时间需要 3 分钟。

文章目录


原题题目

在这里插入图片描述



代码实现(首刷超暴力解法)超时

int profit;void calculateprofit(int* prices,int pricesSize,int pos,int shares,int fee,int money){
if(money > profit) profit = money; if(pos < pricesSize) {
if(!shares) calculateprofit(prices,pricesSize,pos+1,prices[pos],fee,money-prices[pos]); else calculateprofit(prices,pricesSize,pos+1,0,fee,money+prices[pos]-fee); calculateprofit(prices,pricesSize,pos+1,shares,fee,money); }}int maxProfit(int* prices, int pricesSize, int fee){
profit = 0; calculateprofit(prices,pricesSize,0,0,fee,0); return profit;}


代码实现(半看解半自解) DP果然不好学

int maxProfit(int* prices, int pricesSize, int fee){
int dp[pricesSize][2],profit = 0,i; dp[0][0] = 0,dp[0][1] = (-prices[0]); for(i=1;i

代码实现(二刷自解 C++)

class Solution {
public: int maxProfit(vector
& prices, int fee) {
int size = prices.size(); int prebuy = -prices[0],presold = 0,nowbuy = 0,nowsold = 0; for(int i=1;i

转载地址:http://fzqvf.baihongyu.com/

你可能感兴趣的文章
JAVA学习笔记9 - 异常
查看>>
JAVA学习笔记10 - 继承
查看>>
JAVA学习笔记11 - 接口interface
查看>>
Android 开发学习笔记 00 - Getting Started
查看>>
【学习笔记】Android Activity
查看>>
Android使用Retrofit_00_Getting Started
查看>>
Android使用Retrofit_01_OAuth2 + GitHub
查看>>
Django oauth toolkit + Android + Retrofit 实现 OAuth2 的 access token 获取
查看>>
Android + Django + OAuth2 + Stub Authenticator
查看>>
Django + REST学习笔记
查看>>
Android Sync Adapter (使用Stub Content Provider) 笔记
查看>>
诡异的 Scroll view may have only one direct child placed within it 错误
查看>>
【转载】将Ubuntu16.04 中gedit在仅显示一个文件时显示文件名tab
查看>>
fstream 对象多次使用时注意clear
查看>>
调试 LenaCV 3D Camera (Linux)
查看>>
OpenCV杂记 - Mat in C++
查看>>
location区段
查看>>
nginx访问控制、基于用户认证、https配置
查看>>
SaltStack
查看>>
linux内存的寻址方式
查看>>