Week 7 - 714. Best Time to Buy and Sell Stock with Transaction Fee & 718. Maximum Length of Repeated Subarray
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的更多相关文章
- 714. Best Time to Buy and Sell Stock with Transaction Fee
问题 给定一个数组,第i个元素表示第i天股票的价格,可执行多次"买一次卖一次",每次执行完(卖出后)需要小费,求最大利润 Input: prices = [1, 3, 2, 8, ...
- 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 ...
- [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 ...
- 【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 ...
- 【LeetCode】714. Best Time to Buy and Sell Stock with Transaction Fee 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- HTTPS和HTTP的区别,http协议的特征
http协议传输的数据都是没有经过加密的,也就是明文,所以http用于传输数据并不安全.而https是是使用了ssl(secure socket layer)协议+http协议构成的可加密传输,身份认 ...
- 雷电模拟器 v3.71绿色版
目录 1. 按 2. 介绍 3. 下载地址 1. 按 安卓模拟器目前的市场基本上稳定了,逍遥.夜神.雷电,用的人都很多,网易mumu从阴阳师时代就被大家熟知,腾讯手游助手也是随着吃鸡游戏而火,这两个模 ...
- 1、Framework7
一. <!DOCTYPE html> <html> <head> <!-- 所需的Meta标签--> <meta charset="ut ...
- 03Java基础——继承
1.继承 例如一个员工类,包括开发员工和经理. package cn.jxufe.java.chapter2.demo12; public class Employee { String name; ...
- 怎么在html动态实现显示和隐藏效果
效果目标图: 这个还是比较好实现的,附源码: <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...
- Python之常用模块一(主要RE和collections)
一.认识模块 什么是模块:一个模块就是一个包含了python定义和声明的文件,文件名就是加上.py的后缀,但其实import加载的模块分为四个通用类别 : 1.使用python编写的代码(.py文件 ...
- 自学semantic UI个人博客首页模板
以下是代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <m ...
- SSM整合中错误:Data truncation: Data too long for column 'gender' at row 1
错误描述 ### SQL: insert into t_customer(name,gender,phone,address) values (?,?,?,?) ### Cause: com.mysq ...
- Python---面向对象---龟鱼游戏
一.定义一个门票系统 门票的原价是100元 当周末的时候门票涨价20% 小孩子半票 计算2个成人和1个小孩的平日票价 ----------------------------------------- ...
- 【NOIP2013模拟】归途与征程
题目 分析 好吧...明显是暴力题. 首先,把A串分成只有小写字母组成的小分串,按顺序存放:A[1].A[2].A[3]--. 对于同构循环串,显然把两个B串合在一起,成为一个新的C串.\(C[i.. ...