leetcode 121. Best Time to Buy and Sell Stock

Realtek 10GbE芯片组解析:低成本高速网络方案 10GbE(万兆以太网)技术通过提升网络带宽,显著改善了数据传输效率,成为现代网络架构的核心组件。其工作原理基于高速串行通信协议,通过优化信号处理和编码技术实现低延迟、高吞吐量。在技术价值上,10GbE不仅满足数据中心和企业网络的高需求,还逐步渗透到消费级市场,推动家庭和小型企业的网络升级。应用场景包括NAS存储、视频编辑工作站和云服务接入等。Realtek最新发布的RTL8127、RTL8159和RTL8261C芯片组,以低功耗和低成本优势,进一步降低了10GbE的普及门槛。这些芯片支持PCIe Gen4 阅读详情

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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.

数组是每天的股票价格,问在最低价买入最高价卖出的收益,注意卖出的时间要在买入的后面

思路:
顺序访问数组,更新最低价,同时更新最大收益(price[i] - 最低价)

    public int maxProfit(int[] prices) {
        if(prices == null || prices.length == 0) {
            return 0;
        }
        
        int minV = prices[0];
        int result = 0;
        
        for(int i = 1; i < prices.length; i++) {
            result = Math.max(result, prices[i] - minV);
            minV = Math.min(minV, prices[i]);
        }
        
        return result;
    }
基于STM32F407的DDS信号源设计与实现:DAC+DMA+TIM的高精度波形生成 本文详细介绍了基于STM32F407微控制器实现高精度DDS信号源的完整方案。通过结合DAC、DMA和TIM外设,并运用DDS算法,实现了分辨率高达0.001Hz的波形生成。文章深入剖析了相位累加器、波表查表等核心原理,提供了从CubeMX配置到代码实战的完整指南,并探讨了性能优化与高频输出问题的解决方案。 阅读详情

相关推荐

遗传病变异注释预测工具安装系列之------Exomiser 安装及bug处理

Exomiser是一个Java程序,它从全外显子组或全基因组测序数据中发现潜在的致病变异。 Java 版本必须为1.8以上。 Exomiser 最新版本下载 wget -c https://github.com/exomiser/Exomiser/releases/download/11.0.0/exomiser-cli-11.0.0-distribution.zip unzip ...

qq_37329349的博客 1537

LeetCode算法题之121. Best Time to Buy and Sell Stock(easy)【Python3动态规划题解】

题目描述: 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 (i.e., buy one and sell one share of the st...

DaMoWangZQ的博客 445

【新手必看】挖漏洞流程指南,黑客手把手教你挖漏洞月入过万!收藏这篇就够了!(附学习笔记)

【新手必看】挖漏洞流程指南,黑客手把手教你挖漏洞月入过万!收藏这篇就够了!(附学习笔记)【新手必看】挖漏洞流程指南,黑客手把手教你挖漏洞月入过万!收藏这篇就够了!(附学习笔记)

weixin_57514792的博客 1105

【Leetcode】121. 最佳买卖股票的时间 I(Best Time to Buy and Sell Stock)

Leetcode - 121 Best Time to Buy and Sell Stock (Easy) 题目描述:最多交易一次。 解法一:DP public int maxProfit(int[] prices) { if(prices == null || prices.length == 0) return 0; int[][] mp = new int[prices.le...

厉兵秣码 176

地瓜派RDK X5部署YOLOv11n避坑指南:从Softmax瓶颈到BPU加速的实战解析

本文详细解析了在地瓜派RDK X5开发板上部署YOLOv11n模型时,从性能暴跌到成功优化的全过程。核心在于通过配置`node_info`将模型中的Softmax算子强制在BPU上以int16精度运行,解决了因算子被分配到CPU导致的严重性能瓶颈。实战部署后,模型推理延迟从126ms降至10.8ms,FPS从7.9提升至92.3,实现了显著的性能优化。

beta5的博客 867

台达PLC DIAdesigner-AX 编程实战:从安装到通信配置

本文详细介绍了台达PLC DIAdesigner-AX编程的实战操作,从软件安装、项目创建到通信配置的全流程。重点讲解了TCP/IP通信设置、库文件管理及常见错误处理,帮助工程师快速掌握台达PLC编程技巧,提升工业自动化项目的开发效率。

weixin_29215391的博客 533

【leetcode】40-best-time-to-buy-and-sell-stock 力扣 121. 买卖股票的最佳时机

解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5。给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。核心的一点:最大的利润,卖出之前则必须是买入的最小值、卖出的最大值。b1 买入时,我们初始化为 -prices[0];输入:prices = [7,6,4,3,1]输入:[7,1,5,3,6,4]

917

短距激光雷达专业讲解:定义、架构、技术路线与选型逻辑

短距激光雷达,通常指有效探测距离不大于 50 米的激光雷达产品。它主要服务于服务机器人、扫地机器人、AGV/AMR、无人机、智能手机、AR/VR、工业安全等中近距感知场景。它与车载中长距激光雷达的最大区别,不是“探测距离短一点”,而是整套工程目标不同。短距激光雷达的核心诉求是:小体积、低成本、高帧率、强抗光、嵌入式集成。单价从百元级扫地机模组,到数千元级工业 3D 雷达不等。一句话概括:中长距车载激光雷达追求远距离、高可靠、车规寿命;

致力于lidar应用开发 151

[LeetCode]121. Best Time to Buy and Sell Stock

[LeetCode]121. Best Time to Buy and Sell Stock题目描述思路 对于原数组,生成一个新数组,对应位置的值表示当前位置到结尾的最大值,之后遍历,获取两个数组的差返回最大值 2.另一种思路,遍历一次,关键:curMax += max(0, curMax += [i] - [i - 1]) ,含义是 更新当前最大值 比如对 1, 2, 3 在指向3时,将当前最大

Lcharon的博客 607

最新Codex接入DeepSeek官方API教程(无需cc-switch)

最新Codex接入DeepSeek官方API教程(无需cc-switch)

猿小白的博客 971

【算能】TPU(张量处理单元)的深度解析

算能(SOPHGO)作为国产AI芯片领域的核心企业,其TPU(张量处理单元)以高能效比与全场景覆盖能力为核心竞争力,专注于边缘计算与行业垂直领域的AI推理加速。

浩瀚之水的专栏 1550

LeetCode 121. Best Time to Buy and Sell Stock

LeetCode 121. Best Time to Buy and Sell StockMaSay 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,

nianhua120的专栏 426

RK3576 Buildroot 双机 ADB 异常排障实录:同版 `boot.img`,一台可识别一台不可识别

进程在跑”不等于“USB 枚举成功”,adbd存活只是必要不充分条件。生产环境中的全局别名可能反向污染系统脚本。shell 命令替换中的字符串问题,必须做字节级验证(od -tx1相比“改环境规避”,核心脚本对输入做鲁棒处理更可长期维护。

小灰灰的博客 214

leetcode题解-121. Best Time to Buy and Sell Stock && 122. Best Time to Buy and Sell St && 66. Plus One

今天的两道题都很简单,就直接在一篇博客里进行记录即可。 首先说一下66. Plus One这道题目:Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading

呜呜哈的博客 479

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

LeetCode 121. Best Time to Buy and Sell Stock 题解

本题是贪心算法的经典应用,也是股票系列问题的入门题目。贪心:维护最低价格,计算最大利润动态规划:状态定义为持有/不持有股票空间优化:只需要维护两个状态通过本题可以深入理解贪心算法和动态规划在股票问题中的应用。

cannonjinx的博客 4040

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Leetcode 121 Best Time to Buy and Sell Stock

Leetcode 121 Best Time to Buy and Sell Stock#include <vector> #include <math.h> using namespace std;class Solution { public: int maxProfit(vector<int>& prices)//brute force time limit exceeded

自己的学习笔记 217

leetcode 121 Best Time to Buy and Sell Stock 详细解答

leetcode 121 Best Time to Buy and Sell Stock 详细解答 此题的思路比较简单, 思路1:在遍历数组的时候保存一个最小值,同时保存一个当前值与最小值的差值,最后的结果就是保留最大的差值 代码如下: 思路2: 从股票的角度来理解,买入一支股票P就相当于自己本金亏损了P, 卖出一支股票P1就相当于自己赚了P1。代码跟上述代码几乎一致,只是思路不同。 ...

qq_22210659的博客 209

Leetcode 121. Best Time to Buy and Sell Stock

Leetcode 121. 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 (i.e...

fengran_mark的博客 140

[leetcode]121. Best Time to Buy and Sell Stock

[leetcode]121. Best Time to Buy and Sell Stock Analysis 周五啦周五啦,网上买的小南瓜到啦—— [六一快乐,嘻嘻~] Say you have an array for which the ith element is the price of a given stock on day i. If you were onl...

weixin_32135877的博客 88

闯关leetcode——121. Best Time to Buy and Sell Stock

实际我们在考虑这个问题时,需要抓住一个点:起始位置(目前的最小值)后如果小于该值的值,则它和后面出现的最大值的差就是最大差;如果出现了比它小的值,就要拿这个小的值向后去尝试。以此类推,可以得出最大的差值。这样它的复杂度就是O(n)。一种直观的思路就是找到一个值后,去寻找它后面最大的值,然后将差保存起来。不停迭代这个过程,得到最大的差值。但是这个思路是O(n。这题就是要找到在一个区间中,计算一个值与后面最大值的差,然后找到最大的差。

方亮的专栏 1353

LeetCode力扣 121. 买卖股票的最佳时机 Best Time to Buy and Sell Stock 题解代码 JavaScript

问题https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/ 练习使用JavaScript解答 /** * @param {number[]} prices * @return {number} */ var maxProfit = function(prices) { if(prices.length < 2) return 0; var arr = []; arr[pr.

Coding home - 漂流瓶jz 513

【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Python)

【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Python)题目描述解法 题目地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 题目描述 Say you have an array for which the ith element is the price ...

ttinch的博客 174

【LeetCode】121.Best Time to Buy and Sell Stock解题报告

【LeetCode】121.Best Time to Buy and Sell Stock解题报告tags: Array 题目地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/#/description 题目描述:   Say you have an array for which the ith element

郝春雨的博客 393

【leetcode 121】Best Time to Buy and Sell Stock

题目: Say you have an array for which theithelement is the price of a given stock on dayi. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the st...

不了痕的博客 1085
上一篇: leetcode 120. Triangle(三角形)
下一篇: leetcode 123. Best Time to Buy and Sell Stock III
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值