【LeetCode】Best Time to Buy and Sell Stock Ⅰ、Ⅱ、Ⅲ【Java】

【kubeEdge】离线部署 根据k8s的这个规则,可以将节点通过 label 的方式进行分组,比如将某几个节点都打上标签为 name=edge,当调度应用到 edge 节点时,会找到 label 为 edge 的所有节点,根据设置的副本数,将应用启动起来,如果设置的副本数超过了设置 edge 标签的节点数,则多出的将会显示 pending 状态,从而无法启动。清除 keadm init 或 keadm join 造成的影响均可使用 keadm reset,这个操作会删除 /etc/kubeedge 下的文件,如有需要请提前备份。 阅读详情

121. Best Time to Buy and Sell Stock

在这里插入图片描述
题意为只可买入一次,并卖出一次,要求获取的利润最高。最简单的思路就是两层循环,比较每一个数与后面的数的差值,以求出当前能获取的最大差值,即为最大利润,算法的时间复杂度即O(n²)。我们可以考虑优化算法,我们每获取一个数时,我们其实只需要跟这个数之前最小的那个数做比较,就可以知道这个为出口时,能获取的最大利润值,再通过比较所有出口的最大利润值,来获取一个全局最优解。

	public int maxProfit(int[] prices) {
        if (prices == null || prices.length <= 1) {
            return 0;
        }

        int minValue = Integer.MAX_VALUE;
        int maxProfit = 0;
        for (int price : prices) {
            if (price < minValue) {
            	// 更新当前的遍历过得数组的最小值
                minValue = price;
            } else if (price - minValue > maxProfit) {
            	// 计算当前位置为出口时的最大利润
            	// 如超过全局最优,则覆盖全局最优
                maxProfit = price - minValue;
            }
        }
        return maxProfit;
    }

122. Best Time to Buy and Sell Stock II

在这里插入图片描述
第二题与第一题的区别在于,现在不需要考虑买卖的次数,可以无数次进行买入卖出。那解题思路变的很容易,我们只需要不断加上数组连续数之间的差值,即能得到一个总的利润(贪心的思想)。代码如下:

	public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) {
            return 0;
        }

        int maxProfit = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] - prices[i - 1] > 0) {
                maxProfit += prices[i] - prices[i - 1];
            }
        }
        return maxProfit;
    }

123. Best Time to Buy and Sell Stock III

在这里插入图片描述
对于第三题,其约束了买入卖出的次数,至多两次。我们可以考虑,将买入卖出2次,看成了四次行为,即买1、卖1、买2、卖2,因为只有手头上的钱才是钱,即购买股票可视为支出(或亏损),即购买 price 时,当日的收益为 buy = -price,而卖出视为收入,收益需要考虑支出,即卖出价位为 price 时,总收益为 buy + price。而,我们第二次的购买,会受到第一次买入结果的影响,因此,第二次购入时,其收益为 sell1 - price。代码如下:

	public int maxProfit(int[] prices) {
        int b1 = Integer.MIN_VALUE, s1 = 0;
        int b2 = Integer.MIN_VALUE, s2 = 0;
        for (int price : prices) {
            // 第一次买入,支出 price
            b1 = Math.max(b1, -price);
            // 第一次卖出,盈利 price + b1
            s1 = Math.max(s1, price + b1);

            // 第二次买入,受第一次买卖的影响,因此有 s1 的本金,支出 price
            b2 = Math.max(b2, s1 - price);
            // 第二次买入,盈利 price + b2 
            s2 = Math.max(s2, price + b2);
        }
        return s2;
    }
【Proteus仿真】51单片机+DAC0832+LM358放大电路波形发生器 阅读详情

相关推荐

macOS 发票查验全攻略:从证书导入到Safari调试

本文详细介绍了在macOS上进行发票查验的全流程,包括证书下载、钥匙串访问导入、Safari浏览器调试等关键步骤。重点解决了验证码不显示等常见问题,提供了证书信任设置和开发者工具使用的实用技巧,帮助用户顺利完成税务发票在线查验。

weixin_29204749的博客 544

【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】

【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Say you have an array for which the ith element is the price of a given stock on day i.   If you were only permitt

https://github.com/Wang-Jun-Chao 1万+

ADF4002分频器避坑指南:常见配置错误及解决方案

本文深入解析了ADF4002分频器在实战应用中的常见陷阱与解决方案。内容涵盖寄存器配置逻辑、硬件PCB布局要点、软件驱动时序细节及系统化调试方法,重点阐述了如何避免因配置错误导致芯片无法正常工作的问题,帮助工程师高效完成高精度频率合成系统设计。

weixin_29192141的博客 95

Leetcode Best Time to Buy and Sell Stock系列题目解析以及java实现

Leetcode Best Time to Buy and Sell Stock系列题目解析以及java实现 这一个系列的题目就是让我们在一段连续的股票价格中根据限制来进行交易从而获得最大的利润,每一个衍生题都会设置一些限制 Best Time to Buy and Sell Stock I Best Time to Buy and Sell Stock II Best Time to Buy and Sell Stock III Best Time to Buy and Sell Stock with

Dh0le的博客 488

飞书文档API深度使用指南:如何用n8n社区节点绕过HTTP Request的复杂配置

本文详细解析了如何利用n8n社区节点简化飞书文档API的集成过程,绕过复杂的HTTP Request配置。通过n8n-feishu-lite社区节点,开发者可以轻松实现飞书文档的创建、编辑和查询操作,大幅提升自动化工作流的效率。文章还提供了高级应用场景和性能优化建议,帮助开发者更好地利用飞书开放平台的API功能。

weixin_29227585的博客 83

MS建模mmt

一、流程:1.定义好每个原子力场类型:Edit Sets(分组) 2.加H(一般是仅与Al-O八面体中的Al相连的O原子)、给H分组 3.“make P1”、校核化学式(正确与否) 4.扩成超晶胞(Na-mmt:4*1*1,Ca-mmt:4*2*1,illite:2*1*1,由化学式而定) 5.置换阳离子: mmt:模型中每 32 个 Si 4+有 1 个被 Al 3+ 取...

weixin_30721899的博客 2174

关于LeetCode中Best Time to Buy and Sell Stock一题的理解

题目如下: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the

zsy112371的专栏 3166

做cellchat画图时自环的角度颠倒,如何解决?

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。

**My Coding Family** 462

【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

Best Time to Buy and Sell Stock I题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求最大的收益。分析:动态规划法。从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求。**代码:**O(n)时

跳出温水的青蛙 2万+

OpenClaw 超级 AI 实战专栏【实战案例】(五)邮件收发 + 日历日程 + 文件管理一站式搞定

OpenClaw 实现办公自动化核心是模块解耦:邮件收发、文件管理、日历日程各自封装,便于维护和扩展;开发中需重点解决编码兼容、权限配置、时间格式、重复处理四大类坑点,核心思路是 “提前校验 + 异常兜底 + 统一格式”;敏感信息(邮箱密码、API Token)务必通过.env管理,避免硬编码泄露。

qq_36130719的博客 739

【云原生系列】第一讲:什么是云计算

云计算(cloud computing):分布式:分布式计算的一种,指的是通过网络“云”将巨大的数据计算处理程序分解成无数个小程序,合并&反馈:通过多部服务器组成的系统进行处理和分析这些小程序得到结果并返回给用户。云计算早期:分布式计算:简单的分布式计算,解决任务分发,并进行计算结果的合并。因而,云计算又称为网格计算。通过分布式计算,可以在很短的时间内(几秒钟)完成对数以万计的数据的处理,从而达到强大的网络服务。现阶段:计算:分布式计算、效用计算、负载均衡、并行计算、存储。

颜淡慕潇 1万+

Best Time to Buy and Sell Stock 【最佳买卖股票的时间】【容易】

一、题目 英文:Best Time to Buy and Sell Stock 中文:最佳买卖股票的时间 二、内容要求 英文: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to comple

hitscutee的博客 495

iOS逆向之深入解析Hook的原理方法和安全防护

Hook简介 Hook就是一种改变程序执行流程的一种技术的统称; 一段程序的执行流程是 A --> B --> C,现在我们在 A 和 B 之间插入一段代码或者直接改变 B ,这样程序原有的执行流程就发生了改变。如下图所示: Hook的方式:Method Swizzle,fishhook,Cydia Substrate; Hook原理 一、Method Swizzle 原理 利用OC的Runtime特性,动态去改变SEL(方法编号)和IMP(方法实现)的对应关系,达到OC方法调用流程改变

╰つ栺尖篴夢ゞ 2万+

leetcode best-time-to-buy-and-sell-stock-iii(Java)

leetcode题目 best-time-to-buy-and-sell-stock-iii – newcoder 28 买卖股票的最佳时机III – leetcode 123 题目描述 * Say you have an array for which the i th element * is the price of a given stock on day i. * * Des...

聚沙成塔 684

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

【LeetCode】解题122:Best Time to Buy and Sell Stock II

LeetCode解题 122:Best Time to Buy and Sell Stock II Problem 122: Best Time to Buy and Sell Stock II [Easy]解题思路Solution (Java) Problem 122: Best Time to Buy and Sell Stock II [Easy] Say you have an array...

Fayedily的博客 367

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

[leetcode] 309. Best Time to Buy and Sell Stock with Cooldown

Description Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i...

Learning from the mistakes 513

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

leetcode best-time-to-buy-and-sell-stock(Java)

leetcode题目 best-time-to-buy-and-sell-stock – newcoder 30 买卖股票的最佳时机 – leetcode 121 题目描述 * Say you have an array for which the i th element * is the price of a given stock on day i. * * If you were...

聚沙成塔 484

leetcode之Best Time to Buy and Sell Stock III 问题

问题描述: Say you have an array for which the ith element is the price of a given stock on dayi. Design an algorithm to find the maximum profit. You may complete at mosttwo transactions.(发现这些问题变化了啥没?)

漫漫学习路 326

LeetCode:188. Best Time to Buy and Sell Stock IV - Python

LeetCode:188. Best Time to Buy and Sell Stock IV(买卖股票的最佳时机 )问题描述:给定一个数组,它的第i个元素是一支给定的股票在第i天的价格。设计一个算法来计算你所能获取的最大利润。你最多可以完成k笔交易。 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。示例1:输入: [2,4,1], k = 2 输出: 2...

Growth Diary 2179

【LeetCode】解题309:Best Time to Buy and Sell Stock with Cooldown(动态规划)

LeetCode解题 309:Best Time to Buy and Sell Stock with Cooldown (动态规划)Problem 309: Best Time to Buy and Sell Stock with Cooldown [Medium]解题思路Solution (Java)修改过程 Problem 309: Best Time to Buy and Sell Sto...

Fayedily的博客 323

[刷题]leetcode #309 - Best Time to Buy and Sell Stock with Cooldown

题目 代码 class Solution { public int maxProfit(int[] prices) { if (prices.length <= 1) return 0; int l = prices.length; int a=0, b = -prices[0], c = 0; for (int i = 1; i < l; ++i){ int temp = a;

qq_27637285的博客 309

【LeetCode】解题123:Best Time to Buy and Sell Stock III(动态规划)

LeetCode解题 123:Best Time to Buy and Sell Stock III (动态规划)Problem 123: Best Time to Buy and Sell Stock III [Hard]解题思路I. O(2n)时间+O(3n)空间II. O(2n)时间+O(n)空间III. O(n)时间+O(4)空间Solution (Java) Problem 123: B...

Fayedily的博客 491

第十周 Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock III LeetCode上的123题:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 文章目录Best Time to Buy and Sell Stock III题目题解代码 题目 给出一个大小为n的数组,表示从1到n天股票的价格。有两次交易机会(...

tmhf_cc的博客 557

[LeetCode ] Best Time to Buy and Sell Stock系列题解

这一系列的题目的基本模型是给出n天每天股票的价格,可以在任意一天买股票,在之后的任意一天把股票卖出,赚取差价,限制是手中最多有一张股票,必选把手中的股票卖掉才能再买股票。求最大获利。 题目1:Best Time to Buy and Sell Stock 这是最简单的一道题,在基本模型的基础上添加了一个限制条件,最多交易一次。 我们从第一天开始遍历,维护一个已经遍历过的股票的最小价格mi...

GYH0730的博客 574

leetcode best-time-to-buy-and-sell-stock-ii(Java)

leetcode题目 best-time-to-buy-and-sell-stock-ii – newcoder 29 买卖股票的最佳时机II-- leetcode 122 题目描述 * Say you have an array for which the i th element is * the price of a given stock on day i. * * Desig...

聚沙成塔 536

LeetCode Top 100 Liked Questions 121. Best Time to Buy and Sell Stock (Java版; Easy)

welcome to my blog LeetCode Top 100 Liked Questions 121. Best Time to Buy and Sell Stock (Java版; Easy) 题目描述 Say you have an array for which the ith element is the price of a given stock on day i. If ...

littlehaes的博客 133
上一篇: 【LeetCode Top 100】406. Queue Reconstruction by Height【Medium】【JAVA】
下一篇: 【LeetCode】130. Surrounded Regions【Medium】【JAVA】
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值