当前位置: 首页 > news >正文

买卖股票的最佳时机 IV - 困难

*************
C++

topic:188. 买卖股票的最佳时机 IV - 力扣(LeetCode)

*************

Stock angin:

Still stocks. Intuitively, it feels hard. 

For once:

class Solution {
public:int maxProfit(vector<int>& prices) {int minPrice = prices[0]; // 迄今为止遇到的最低价格int maxPro = 0; // 最大利润for (int i = 1; i < prices.size(); ++i) {// 如果当前价格比迄今为止的最低价格还低,更新最低价格if (prices[i] < minPrice) {minPrice = prices[i];} else {// 如果当前价格比最低价格高,计算利润,并更新最大利润maxPro = max(maxPro, prices[i] - minPrice);}}return maxPro;}
};

For twice.

class Solution {
public:int maxProfit(vector<int>& prices) {int n = prices.size(); // get the lengthint firstBuy = - prices[0];int firstSale = 0;int secondBuy = - prices[0];int secondSale = 0;// do sth. herefor (int i = 1; i < n; i++){firstBuy = max(firstBuy, - prices[i]);firstSale = max(firstSale, prices[i] + firstBuy);// the second salesecondBuy = max(secondBuy, firstSale - prices[i]);secondSale = max(secondSale, prices[i] + secondBuy);}return secondSale;}
};

For three times:
 

class Solution {
public:int maxProfit(vector<int>& prices) {if (prices.empty()) return 0;int n = prices.size();// 初始化状态变量int firstBuy = -prices[0];  // 第一次买入的最大利润int firstSell = 0;          // 第一次卖出的最大利润int secondBuy = -prices[0]; // 第二次买入的最大利润int secondSell = 0;         // 第二次卖出的最大利润int thirdBuy = -prices[0];  // 第三次买入的最大利润int thirdSell = 0;          // 第三次卖出的最大利润for (int i = 1; i < n; ++i) {// 更新第一次买入的最大利润firstBuy = max(firstBuy, -prices[i]);// 更新第一次卖出的最大利润firstSell = max(firstSell, firstBuy + prices[i]);// 更新第二次买入的最大利润secondBuy = max(secondBuy, firstSell - prices[i]);// 更新第二次卖出的最大利润secondSell = max(secondSell, secondBuy + prices[i]);// 更新第三次买入的最大利润thirdBuy = max(thirdBuy, secondSell - prices[i]);// 更新第三次卖出的最大利润thirdSell = max(thirdSell, thirdBuy + prices[i]);}return thirdSell;}
};

seems to have some idea.

I always think about one thing, if I had a mirror space, I could spare a whole life to learn how to code. After that, I enter another space to learn how to design. And go on studying playing balls and so on. However, I have only one time in this world and what I want to learn is too much with lazy mind. Rue always happen. I do some videos to record my travel and rember peoples whit me. Attracting fans sounds good but self happy means everythin. And guess what, I'll go on. Spare time from tiktok to code or work need some courage, and now I have some. All day's work just prove the plan not work, that's works do.

Chris Gardner, a stock trader, played in stocks. In the persuit of happyness, to be honest, I like this moment. Say yes to himself, in variable lives.

back to the topic, idea is find the fimilar things.

class Solution {
public:int maxProfit(vector<int>& prices) {if (prices.empty()) return 0;int n = prices.size();// 初始化状态变量int firstBuy = -prices[0];  // 第一次买入的最大利润int firstSell = 0;          // 第一次卖出的最大利润int secondBuy = -prices[0]; // 第二次买入的最大利润int secondSell = 0;         // 第二次卖出的最大利润int thirdBuy = -prices[0];  // 第三次买入的最大利润int thirdSell = 0;          // 第三次卖出的最大利润int No.Buy = - prices[0];int No.Sale = 0;return thirdSell;}
};

the loop could do the same:

class Solution {
public:int maxProfit(vector<int>& prices) {if (prices.empty()) return 0;int n = prices.size();// 初始化状态变量int firstBuy = -prices[0];  // 第一次买入的最大利润int firstSell = 0;          // 第一次卖出的最大利润int secondBuy = -prices[0]; // 第二次买入的最大利润int secondSell = 0;         // 第二次卖出的最大利润int thirdBuy = -prices[0];  // 第三次买入的最大利润int thirdSell = 0;          // 第三次卖出的最大利润int No.Buy = - prices[0];int No.Sale = 0;for (int i = 1; i < n; ++i) {// 更新第一次买入的最大利润firstBuy = max(firstBuy, -prices[i]);// 更新第一次卖出的最大利润firstSell = max(firstSell, firstBuy + prices[i]);// 更新第二次买入的最大利润secondBuy = max(secondBuy, firstSell - prices[i]);// 更新第二次卖出的最大利润secondSell = max(secondSell, secondBuy + prices[i]);// 更新第三次买入的最大利润thirdBuy = max(thirdBuy, secondSell - prices[i]);// 更新第三次卖出的最大利润thirdSell = max(thirdSell, thirdBuy + prices[i]);No.Buy = max(No.Buy, No.-1Buy - prices[I]);
No.Sale = max(No.Sale, No.Sale + prices[I]);}return No.Sell;}
};

Even people who doesnot know the code knows that the code doesnot runs, need some magic.

Now have two variables, i day and k times.

Define dp[i][j], the maximum profit in i days and trade j times. Then the most signifiicant eauation can be writed as 

dp[i][j][sale] = max(dp[i-1][j][0], dp[i-1][j][1] + prices[i])dp[i][j][buy] = max(dp[i-1][j][1], dp[i-1][j-1][0] - prices[i])

This method can easilly understood while the array comes to three-dimentional which is really hard to caculated. I can solve two-dimentional array problems. However, the three is a little bit hard.

For each day, you have two options, have the stock or donot have the stock.

For have the stock, you may have it yesterday or buy it today. So the maximum profit is

 buy[j] = max(buy[j], sell[j - 1] - prices[i]);

For donot have the stock, you may sold it yesterday or sold it today, so the maximum profit is

sell[j] = max(sell[j], buy[j] + prices[i]);

Maybe it works but I am not sure, take a example:

prices = {3, 2, 6, 5, 0, 3};
k = 2

day 0, price is 3

  • j=1:
  • buy[1] = max(buy[1], sell[0]-3) = max(INT_MIN, 0-3) = -3
  • sell[1] = max(sell[1], buy[1]+3) = max(0, -3+3) = 0
  • j=2:
  • buy[2] = max(buy[2], sell[1]-3) = max(INT_MIN, 0-3) = -3
  • sell[2] = max(sell[2], buy[2]+3) = max(0, -3+3) = 0

day 1,price = 2

  • j=1:
  • buy[1] = max(-3, sell[0]-2) = max(-3, 0-2) = -2
  • sell[1] = max(0, -2+2) = 0
  • j=2:
  • buy[2] = max(-3, sell[1]-2) = max(-3, 0-2) = -2
  • sell[2] = max(0, -2+2) = 0

day 2,price = 6:

  • j=1:
  • buy[1] = max(-2, 0-6) = -2
  • sell[1] = max(0, -2+6) = 4
  • j=2:
  • buy[2] = max(-2, 4-6) = -2
  • sell[2] = max(0, -2+6) = 4

day 3,price = 5:

  • j=1:
  • buy[1] = max(-2, 0-5) = -2
  • sell[1] = max(4, -2+5) = 4
  • j=2:
  • buy[2] = max(-2, 4-5) = -2
  • sell[2] = max(4, -2+5) = 4

day 4,price = 0:

  • j=1:
  • buy[1] = max(-2, 0-0) = 0
  • sell[1] = max(4, 0+0) = 4
  • j=2:
  • buy[2] = max(-2, 4-0) = 4
  • sell[2] = max(4, 4+0) = 4

day 5,price = 3:

  • j=1:
  • buy[1] = max(0, 0-3) = 0
  • sell[1] = max(4, 0+3) = 4
  • j=2:
  • buy[2] = max(4, 4-3) = 4
  • sell[2] = max(4, 4+3) = 7

OK, it works.

class Solution {
public:int maxProfit(int k, vector<int>& prices) {int n = prices.size();vector<int> buy(k + 1, INT_MIN), sell(k + 1, 0);for (int i = 0; i < n; i++) {for (int j = 1; j <= k; j++) {// 更新买入状态,即在第i天买入第j次的最大利润buy[j] = max(buy[j], sell[j - 1] - prices[i]);// 更新卖出状态,即在第i天卖出第j次的最大利润sell[j] = max(sell[j], buy[j] + prices[i]);}}// 最后返回卖出状态的最大值,即进行k次交易的最大利润return sell[k];}
};

This week having some works to do. To be honest, this tipic is a little bit hard which takes me few days to work it. 
 

Anyway, happy weekend.

相关文章:

买卖股票的最佳时机 IV - 困难

************* C topic&#xff1a;188. 买卖股票的最佳时机 IV - 力扣&#xff08;LeetCode&#xff09; ************* Stock angin: Still stocks. Intuitively, it feels hard. For once: class Solution { public:int maxProfit(vector<int>& prices) {in…...

linux源码编译php提示:No package ‘oniguruma‘ found

编译遇到缺少Oniguruma开发包&#xff0c;处理办法1 安装epel仓库、安装 Oniguruma 开发包 [rootiZwz98gb9fzslgpnomg3ceZ php-8.1.29]# yum install epel-release [rootiZwz98gb9fzslgpnomg3ceZ php-8.1.29]# yum install oniguruma oniguruma-devel 方法2&#xff1a;去On…...

2024技能大赛Vue流程复现

1. 关于版本的控制 vue/cli 5.0.8vscode 最新下载版本 2. 创建vuecli项目 若没有安装vuecli则可以先安装 npm install -g vue/cli # 默认下载最新版本。vue --version vue -V # 查看版本&#xff0c;两个选一 使用vuecli来创建一个新的vue项目&#xff0c;vs code打开…...

MATLAB截取图像的一部分并保存导出,在itksnap中3D展示

**问题描述&#xff1a;**输入nifti图像&#xff0c;截取图像的一部分并输出&#xff0c;比如截取图像的101010这一块&#xff0c;并导出为nii文件 inputFile D:\aa\dcm\input.nii; % 输入文件路径subsetSize [10 10 10]; % 截取的图像块大小 subsetStart [1 1 1]; % 截取的…...

JMeter配置原件-计数器

一、面临的问题&#xff1a; 由于本人的【函数助手对话框】中counter计数器每次加2&#xff0c;且只显示偶数(如下图所示)&#xff0c;因此借助【配置原件-计数器】来实现计数功能。 如果有大佬知道解决方式&#xff0c;麻烦评论区解答一下&#xff0c;谢谢。 二、配置原件-c…...

go面试问题

1 Go的内存逃逸如何分析 go build -gcflags-m main_pointer.go 2 http状态码 300 请求的资源可包括多个位置&#xff0c;相应可返回一个资源特征与地址的列表用于用户终端&#xff08;例如&#xff1a;浏览器&#xff09;选择 301 永久移动。请求的资源已被永久的移动到新U…...

springboot 配置Kafka 关闭自启动连接

这里写自定义目录标题 springboot 配置Kafka 关闭自启动连接方法一&#xff1a;使用 ConditionalOnProperty方法二&#xff1a;手动管理Kafka监听器容器方法三&#xff1a;使用 autoStartupfalse结语 springboot 配置Kafka 关闭自启动连接 在Spring Boot应用程序中&#xff0c…...

selenium工作原理

原文链接&#xff1a;https://blog.csdn.net/weixin_67603503/article/details/143226557 启动浏览器和绑定端口 当你创建一个 WebDriver 实例&#xff08;如 webdriver.Chrome()&#xff09;时&#xff0c;Selenium 会启动一个新的浏览器实例&#xff0c;并为其分配一个特定的…...

day14-16系统服务管理和ntp和防火墙

一、自有服务概述 服务是一些特定的进程&#xff0c;自有服务就是系统开机后就自动运行的一些进程&#xff0c;一旦客户发出请求&#xff0c;这些进程就自动为他们提供服务&#xff0c;windows系统中&#xff0c;把这些自动运行的进程&#xff0c;称为"服务" window…...

Hadoop、Hbase使用Snappy压缩

1. 前期准备 系统环境&#xff1a;centos7.9 配置信息&#xff1a;8C8G100G hadoop和hbase为单节点部署模式 jdk版本jdk1.8.0_361 1.1. 修改系统时间 timedatectl set-timezone <TimeZone> 1.2. 修改主机名以及主机名和IP的映射 vim /etc/hosts #将自己的主机名以及…...

【python】OpenCV—Image Moments

文章目录 1、功能描述2、图像矩3、代码实现4、效果展示5、完整代码6、涉及到的库函数cv2.moments 7、参考 1、功能描述 计算图像的矩&#xff0c;以质心为例 2、图像矩 什么叫图像的矩&#xff0c;在数字图像处理中有什么作用&#xff1f; - 谢博琛的回答 - 知乎 https://ww…...

环境变量的知识

目录 1. 环境变量的概念 2. 命令行参数 2.1 2.2 创建 code.c 文件 2.3 对比 ./code 执行和直接 code 执行 2.4 怎么可以不带 ./ 2.4.1 把我们的二进制文件拷贝到 usr/bin 路径下&#xff0c;也不用带 ./ 了 2.4.2 把我们自己的路径添加到环境变量里 3. 认识PATH 3.…...

ATECLOUD测试平台有哪些功能?

测试方案搭建 可视化拖拽编程&#xff1a;采用零代码的图文拖拽形式&#xff0c;用户通过鼠标拖拽节点和连线&#xff0c;即可可视化地构建测试模型&#xff0c;无需懂得代码开发&#xff0c;15 分钟左右就能快速搭建项目&#xff0c;大大降低了上手门槛。 子项目多层嵌套&am…...

使用pyinstaller打包pyqt的程序,运行后提示ModuleNotFoundError: No module named ‘Ui_main‘

环境&#xff1a;windowpython3.9pyqt6 使用pyqt UI编辑器生成了main.ui &#xff0c;main.ui编译成了Ui_main.py main.py 使用当前目录下的Ui_main.py。 打包过程没报错&#xff0c;运行报错。 错误如下: 解决方法&#xff1a;pyinstaller -Fw main.py --paths. 使…...

搭建分布式Spark集群

title: 搭建分布式Spark集群 date: 2024-11-29 12:00:00 categories: - 服务器 tags: - Spark - 大数据搭建分布式Spark集群 本次实验环境&#xff1a;Centos 7-2009、Hadoop-3.1.4、JDK 8、Zookeeper-3.6.3、scala-2.11.5、Spark-3.2.1 功能规划 MasterSlave1Slave2主节点…...

Django基础 - 01入门简介

一、 基本概念 1.1 Django说明 Django发布于2005年&#xff0c; 网络框架&#xff0c; 用Python编写的开源的Web应用框架。采用了MVC框架模式&#xff0c;也称为MTV模式。官网&#xff1a; https://www.djangoproject.com1.2 MVC框架 Model&#xff1a; 封装和数据库相关…...

简单的bytebuddy学习笔记

简单的bytebuddy学习笔记 此笔记对应b站bytebuddy学习视频进行整理&#xff0c;此为视频地址&#xff0c;此处为具体的练习代码地址 一、简介 ByteBuddy是基于ASM (ow2.io)实现的字节码操作类库。比起ASM&#xff0c;ByteBuddy的API更加简单易用。开发者无需了解class file …...

【服务端】Redis 内存超限问题的深入探讨

在 Java 后端开发中&#xff0c;Redis 内存超限是一个常见的问题&#xff0c;可能由多种原因引起。理解这些原因以及如何处理已经超出限制的数据对于保持系统的稳定性和性能至关重要。 一、Redis 内存超限的原因分析 Redis 是一个高性能的内存键值对存储系统&#xff0c;它在…...

Springboot logback 日志打印配置文件,每个日志文件100M,之后滚动到下一个日志文件,日志保留30天(包含traceid)

全部配置 logback.xml <?xml version"1.0" encoding"UTF-8"?> <configuration debug"false"><property name"LOG_HOME" value"log"/><property name"LOG_NAME" value"admin"/&g…...

《计算机组成及汇编语言原理》阅读笔记:p1-p8

《计算机组成及汇编语言原理》学习第 1 天&#xff0c;p1-p8 总结&#xff0c;总计 8 页。 一、技术总结 1.Intel 8088 microprocessor(微处理器)&#xff0c; 1979-1988。 2.MS-DOS Microsoft Disk Operating System的缩写&#xff0c;是一个操作系统(operating system)。…...

变量 varablie 声明- Rust 变量 let mut 声明与 C/C++ 变量声明对比分析

一、变量声明设计&#xff1a;let 与 mut 的哲学解析 Rust 采用 let 声明变量并通过 mut 显式标记可变性&#xff0c;这种设计体现了语言的核心哲学。以下是深度解析&#xff1a; 1.1 设计理念剖析 安全优先原则&#xff1a;默认不可变强制开发者明确声明意图 let x 5; …...

XCTF-web-easyupload

试了试php&#xff0c;php7&#xff0c;pht&#xff0c;phtml等&#xff0c;都没有用 尝试.user.ini 抓包修改将.user.ini修改为jpg图片 在上传一个123.jpg 用蚁剑连接&#xff0c;得到flag...

HTML 语义化

目录 HTML 语义化HTML5 新特性HTML 语义化的好处语义化标签的使用场景最佳实践 HTML 语义化 HTML5 新特性 标准答案&#xff1a; 语义化标签&#xff1a; <header>&#xff1a;页头<nav>&#xff1a;导航<main>&#xff1a;主要内容<article>&#x…...

调用支付宝接口响应40004 SYSTEM_ERROR问题排查

在对接支付宝API的时候&#xff0c;遇到了一些问题&#xff0c;记录一下排查过程。 Body:{"datadigital_fincloud_generalsaas_face_certify_initialize_response":{"msg":"Business Failed","code":"40004","sub_msg…...

FFmpeg 低延迟同屏方案

引言 在实时互动需求激增的当下&#xff0c;无论是在线教育中的师生同屏演示、远程办公的屏幕共享协作&#xff0c;还是游戏直播的画面实时传输&#xff0c;低延迟同屏已成为保障用户体验的核心指标。FFmpeg 作为一款功能强大的多媒体框架&#xff0c;凭借其灵活的编解码、数据…...

MVC 数据库

MVC 数据库 引言 在软件开发领域,Model-View-Controller(MVC)是一种流行的软件架构模式,它将应用程序分为三个核心组件:模型(Model)、视图(View)和控制器(Controller)。这种模式有助于提高代码的可维护性和可扩展性。本文将深入探讨MVC架构与数据库之间的关系,以…...

WordPress插件:AI多语言写作与智能配图、免费AI模型、SEO文章生成

厌倦手动写WordPress文章&#xff1f;AI自动生成&#xff0c;效率提升10倍&#xff01; 支持多语言、自动配图、定时发布&#xff0c;让内容创作更轻松&#xff01; AI内容生成 → 不想每天写文章&#xff1f;AI一键生成高质量内容&#xff01;多语言支持 → 跨境电商必备&am…...

select、poll、epoll 与 Reactor 模式

在高并发网络编程领域&#xff0c;高效处理大量连接和 I/O 事件是系统性能的关键。select、poll、epoll 作为 I/O 多路复用技术的代表&#xff0c;以及基于它们实现的 Reactor 模式&#xff0c;为开发者提供了强大的工具。本文将深入探讨这些技术的底层原理、优缺点。​ 一、I…...

有限自动机到正规文法转换器v1.0

1 项目简介 这是一个功能强大的有限自动机&#xff08;Finite Automaton, FA&#xff09;到正规文法&#xff08;Regular Grammar&#xff09;转换器&#xff0c;它配备了一个直观且完整的图形用户界面&#xff0c;使用户能够轻松地进行操作和观察。该程序基于编译原理中的经典…...

稳定币的深度剖析与展望

一、引言 在当今数字化浪潮席卷全球的时代&#xff0c;加密货币作为一种新兴的金融现象&#xff0c;正以前所未有的速度改变着我们对传统货币和金融体系的认知。然而&#xff0c;加密货币市场的高度波动性却成为了其广泛应用和普及的一大障碍。在这样的背景下&#xff0c;稳定…...