1. 题目要求

Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee representing a transaction fee.

You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. You may not buy more than 1 share of a stock at a time (ie. you must sell the stock share before you buy again.)

Return the maximum profit you can make.

2. Code

DP解法:

  pay用于计算买入价格

  sell用于计算当前利润

  DP的算法是,假定我们当前买入第一天的股票,开销是 【第一天价格】+手续费。利润0。接下来是类似滑窗的设计,数组中,只有i-1和i中的数值是有效的,其中i-1记录着之前的开销,i位置则是当前i-1处的利润-i处的价格+手续费,如果赚到的钱在买了新股,并交了手续费后赚到了钱,就买入。i位记录新的数值,否则就不买,i位记录之前的开销。这样保证买在低点。(开销为负)

  接下来的利润为开销+当前卖出价,i-1初始为0,i处则是i-1买入价格+i卖出价格,即当前的balance,这样计算可以保证卖在高点。

public int maxProfit(int[] prices, int fee) {
int res = 0;
int size = prices.length;
int[] pay = new int[size];
int[] sell = new int[size]; pay[0] = 0 - prices[0] - fee; for(int i = 0 ; i < prices.length ; i ++) { pay[i] = Math.max(pay[i - 1], sell[i - 1] - prices[i] - fee);
sell[i] = Math.max(sell[i - 1], pay[i - 1] + prices[i]); } return sell[size - 1];
}

Leetcode 714 - Node的更多相关文章

  1. [LeetCode] Delete Node in a BST 删除二叉搜索树中的节点

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  2. [LeetCode] Delete Node in a Linked List 删除链表的节点

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  3. Leetcode Delete Node in a Linked List

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  4. Leetcode: Delete Node in a BST

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  5. LeetCode Delete Node in a Linked List (删除链表中的元素)

    题意:给一个将要删除的位置的指针,要删除掉该元素.被删元素不会是链尾(不可能删得掉). 思路:将要找到前面的指针是不可能了,但是可以将后面的元素往前移1位,再删除最后一个元素. /** * Defin ...

  6. LeetCode——Delete Node in a Linked List

    Description: Write a function to delete a node (except the tail) in a singly linked list, given only ...

  7. [LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee 买卖股票的最佳时间有交易费

    Your are given an array of integers prices, for which the i-th element is the price of a given stock ...

  8. leetcode 714. 买卖股票的最佳时机含手续费

    继承leetcode123以及leetcode309的思路,,但应该也可以写成leetcode 152. 乘积最大子序列的形式 class Solution { public: int maxProf ...

  9. LeetCode——714. 买卖股票的最佳时机含手续费.

    给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 :非负整数 fee 代表了交易股票的手续费用. 你可以无限次地完成交易,但是你每次交易都需要付手续费.如果你已经购买了一个 ...

随机推荐

  1. Kibana --> Getting Started -->Building your own dashboard

    https://www.elastic.co/guide/en/kibana/6.6/tutorial-build-dashboard.html Building your own dashboard ...

  2. 【Dalston】【第六章】API服务网关(Zuul) 下

    Zuul给我们的第一印象通常是这样:它包含了对请求的路由和过滤两个功能,其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础.过滤器功能则负责对请求的处理过程进行干预,是实 ...

  3. window下的Django入门

    一.window下新建安装(参考书籍:<python编程:从入门到实践>) 新建一个文件夹 learning_log ,在终端中切换到该目录下,并创建一个虚拟工作环境,运行模块 venv  ...

  4. HashMap分析

    原文链接:http://www.cnblogs.com/chengxiao/p/6059914.html 一.什么是哈希表 在讨论哈希表之前,我们先大概了解下其他数据结构在新增,查找等基础操作执行性能 ...

  5. Java 字符串常用操作(String类)

    字符串查找 String提供了两种查找字符串的方法,即indexOf与lastIndexOf方法. 1.indexOf(String s) 该方法用于返回参数字符串s在指定字符串中首次出现的索引位置, ...

  6. Codeforces 242 E. XOR on Segment

    题目链接:http://codeforces.com/problemset/problem/242/E 线段树要求支持区间求和,区间内每一个数字异或上一个值. 既然是异或,考虑每一个节点维护一个长度为 ...

  7. "ProgrammerHome"项目笔记

    系统目的: 1.技术练习:把平时不用的,重要技术栈,在此项目中打磨(java.python.算法.系统构架) 2.新技术(工具)应用:有些平时想做,想实现的技术,可以在这里实现.而且以微服务的方式,轻 ...

  8. mysql利用navicat导出表结构和表中数据

    LZ在网上搜索了要如何导出mysql的表结构和表中数据,发现有的方法不好用 记录一下好用的方式: 用navicat打开DB链接后,点击数据库,右击选择转储SQL文件,然后选择结构和数据: 之后弹出新的 ...

  9. html5实现获取地理位置信息并定位

    这里主要讲h5实现获取地理位置信息并定位功能,本文讲解了原生h5,百度地图,谷歌地图等三种获取地理信息并定位的方法,需要的朋友可以参考下: h5提供了地理位置功能(Geolocation API),能 ...

  10. redflag的echarts结构

    总体的市场情况 这里我需要4个data数组. var list = { currentData:[],//这里表示当月数据量,数组长度8 totalData:[],//这里表示的累计数据量,数组长度8 ...