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. TCP即时小通信

    package 第十二章; import java.io.*; import java.net.*; public class TcpServer { public static void main( ...

  2. 什么是CPC,CPA,CVR,CTR,ROI

    合格的网络营销人员都应该熟悉下面的常见英文缩写,这些都是我们必须知道的名词解释:CVR (Click Value Rate): 转化率,衡量CPA广告效果的指标CTR (Click Through R ...

  3. 关于阅读Struts2部分拦截器源码的记录

    Struts2中的拦截器在ActionInvocation对象的invoke()方法中执行. ActionInvocation对象从配置文件中读取Interceptor对象,加入到自己的存取拦截器的容 ...

  4. python 写matlab中的加性高斯白噪声AWGN

    定义 原始信号:x 噪声信号:n 信噪比:SNR 信号长度:N def wgn(x, snr): snr = 10**(snr/10.0) xpower = np.sum(x**2)/len(x) n ...

  5. 理解长短期记忆网络(LSTM NetWorks)

    转自:http://www.csdn.net/article/2015-11-25/2826323 原文链接:Understanding LSTM Networks(译者/刘翔宇 审校/赵屹华 责编/ ...

  6. pandas 的index用途

    # pandas的索引index的用途 # 把数据存储于普通的column列也能用于数据查询,那使用index有什么好处? # 1.更方便的数据查询 # 2.使用index可以获得性能提升 # 3. ...

  7. 详解InitializingBean、initMethod和@PostConstruct

    转载:https://blog.csdn.net/nrsc272420199/article/details/95033223 1. InitializingBean.initMethod和@Post ...

  8. “HTTP 错误 404.15 - Not Found 请求筛选模块被配置为拒绝包含的查询字符串过长的请求”之解决办法

    今天同事在做通过接口访问数据时,由于提交的一个参数内容比较多,导致测试时报了以下错误. 同时页面又给出了以下提示: 所以最终根据在网上找了相关资料总结出一下解决办法. 1. 在Web.config配置 ...

  9. POI2012 ODL-Distance

    链接P3532 [POI2012]ODL-Distance 设\(f_{i,j}\)表示他给定的函数,\(g_i\)表示\(i\)的质因数个数 那么\[f_{i,j}=g_{\frac {i*j}{g ...

  10. redis面试题集錦

    1为什么Redis需要把所有数据放到内存中? Redis为了达到最快的读写速度将数据都读到内存中,并通过异步的方式将数据写入磁盘.所以Redis具有快速和数据持久化的特性.如果不将数据放到内存中,磁盘 ...