714. Best Time to Buy and Sell Stock with Transaction Fee - Medium

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.

Example 1:

Input: prices = [1, 3, 2, 8, 4, 9], fee = 2
Output: 8
Explanation: The maximum profit can be achieved by:
Buying at prices[0] = 1
Selling at prices[3] = 8
Buying at prices[4] = 4
Selling at prices[5] = 9
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

Note:

  • 0 < prices.length <= 50000.

  • 0 < prices[i] < 50000.

  • 0 <= fee < 50000.

My Solution:

#include<vector>
#include<iostream>
using namespace std;
class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
int p1 = 0, p2 = INT32_MIN;
for (int i = 0; i < prices.size(); i++) {
int temp = p1;
p1 = max(p1, p2 + prices[i]);
p2 = max(p2, temp - prices[i] - fee);
}
return p1;
} int max(int i, int j) {
if (i >= j) return i; else return j;
}
};

这道题主要的思想是动态规划,难点在于如何想到用两个状态来进行迭代。用一次循环扫整个价格数组,用p1记录无库存状态下的利润,p2记录有库存状态下的利润,当买入时更新p2,卖出时更新p1

718. Maximum Length of Repeated Subarray - Medium

Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.

Example 1:

Input:
A: [1,2,3,2,1]
B: [3,2,1,4,7]
Output: 3
Explanation:
The repeated subarray with maximum length is [3, 2, 1].

Note:

  • 1 <= len(A), len(B) <= 1000
  • 0 <= A[i], B[i] < 100

my solution:

#include<vector>
#include<iostream>
using namespace std;
class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
vector<int> D(B.size()+1, 0);
vector<vector<int>> C(A.size() + 1, D);
int res = 0;
for (int i = 0; i < A.size(); i++) {
for (int j = 0; j < B.size(); j++) {
if (A[i] == B[j]) {
C[i + 1][j + 1] = C[i][j] + 1;
}
if (C[i + 1][j + 1] > res) res = C[i + 1][j + 1];
}
}
return res;
}
};

考虑这样一个表格:

如果两个字符串中有一个子串相同,那么在这个表格中,这个子串就会以斜线的方式显示。利用这个性质对子串的长度计数,就可以得到这道题的动态规划解。

Week 7 - 714. Best Time to Buy and Sell Stock with Transaction Fee & 718. Maximum Length of Repeated Subarray的更多相关文章

  1. 714. Best Time to Buy and Sell Stock with Transaction Fee

    问题 给定一个数组,第i个元素表示第i天股票的价格,可执行多次"买一次卖一次",每次执行完(卖出后)需要小费,求最大利润 Input: prices = [1, 3, 2, 8, ...

  2. 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 ...

  3. [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 ...

  4. 【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 ...

  5. 【LeetCode】714. Best Time to Buy and Sell Stock with Transaction Fee 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  6. Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee)

    Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee) 股票问题: 1 ...

  7. [LeetCode] 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. [Swift]LeetCode714. 买卖股票的最佳时机含手续费 | 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 ...

  9. 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 ...

随机推荐

  1. 锋利的jQuery ——jQuery选择器(二)

    一.jQuery选择器 1)CSS选择器 CSS选择器有:1>标签选择器  E{CSS规则} 2>ID选择器   #ID{CSS规则} 3>类选择器  E.className{CSS ...

  2. javascript 输入框监听事件

    <div class="coupon-exchange clearfix"> <div class="code-input"> < ...

  3. centos虚拟机配置网卡连接

    本地连接 centos虚拟机连接设置: 换过ip之后需要重启网络服务新ip才生效 #service network restart 修改dns:

  4. GIT 开发流程

    1.git clone 使用 git clone 将一个项目下载到本地 2.git checkout -b branchName 新建一个branchName的本地分支 3.git add file/ ...

  5. 牛客练习赛14 A n的约数 (数论)

    链接:https://ac.nowcoder.com/acm/contest/82/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288 ...

  6. centos7安装mxnet

    pip install mxnet-cu90 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com 安装sklearn时总报错 ...

  7. Python核心技术与实战——四|Python黑箱:输入与输出

    抽象的看,Python程序可以被看成一个黑箱:通过输入流将数据送达,经过处理后在输入,也就是说具备了一个图灵机运作的必要条件. 输入输出基础 最简单的输入是来自键盘的操作 name = input(' ...

  8. 微信 PHP - SDK 包

    下载 个人公众号谢谢各位老铁支持

  9. bzoj2600 [Ioi2011]ricehub 双指针

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2600 题解 随便写一个比较简单的 two pointers 练习题. 首先答案肯定是一个原序列 ...

  10. LOJ bitset+分块 大内存毒瘤题

    题面 $ solution: $ 真的没有想到可以用分块. 但是可以发现一个性质,每个询问只关心这个点最后一次赋值操作,和这个赋值操作后的所有取 $ min $ 操作.这个感觉很有用,但是真的很难让人 ...