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

深入理解C++23的Deducing this特性(上):基础概念与语法详解 C++23标准中引入的"Deducing this"特性是一项重大革新,它不仅改变了我们编写成员函数的方式,还为模板元编程和设计模式实现带来了新的可能。 阅读详情

Problem:
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

Solution1:
对数组进行遍历,一一比较

class Solution {
    public int maxProfit(int[] prices) {
        int max = 0;
        for(int i = 0; i < prices.length; i++) {
            for(int j = i + 1; j < prices.length; j++) {
                if(prices[i] <= prices[j]) {
                   max = Math.max(prices[j] - prices[i], max); 
                }
            }
        }
        return max;
    }
}

Solution2:
寻找最小price,同时更新最大profit,只需要遍历一次

class Solution {
    public int maxProfit(int[] prices) {
        int minPrice = Integer.MAX_VALUE;
        int maxProfit = 0;
        for (int i = 0; i < prices.length; i++) {
            if(prices[i] <= minPrice) {
                minPrice = prices[i];
            } else if(prices[i] - minPrice >= maxProfit) {
                maxProfit = prices[i] - minPrice;
            }
        }
        return maxProfit;
    }
}

Extra Material: https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/solution/yi-ge-fang-fa-tuan-mie-6-dao-gu-piao-wen-ti-by-l-3/

AI Agent ReAct架构设计模型剖析 ReAct通过"思考-行动-观察"循环运作:智能体先进行自然语言推理,再调用外部工具执行行动,观察结果后继续推理直至解决问题。这种架构提高了透明度和准确性,减少幻觉风险,使LLM能主动分解任务并利用外部数据源。实现关键在于精心设计的提示模板,引导LLM按结构化格式响应,而非依赖模型固有能力。 阅读详情

相关推荐

Mac上IDEA的PlantUML插件报错‘找不到Graphviz’?手把手教你用Homebrew搞定(附阿里云镜像避坑)

本文详细解决了Mac上IDEA的PlantUML插件报错‘找不到Graphviz’的问题,通过Homebrew安装Graphviz并配置阿里云镜像源,特别针对Apple Silicon芯片用户提供了优化方案。文章还涵盖了IDEA中PlantUML插件的正确配置和常见问题排查,帮助开发者高效使用PlantUML进行UML图表设计。

weixin_42619742的博客 475

[buuctf] crypto全解——121-146(不建议直接抄flag)

[BJDCTF2020]编码与调制 查看题目 曼彻斯特 6进制024A447B4469664D616E63686573746572636F64657D 通过解码16进制即可得到flag{DifManchestercode} [网鼎杯 2020 青龙组]you_raise_me_up 查看题目 #!/usr/bin/env python # -*- coding: utf-8 -*- from Crypto.Util.number import * import random n = 2 ** 512 m

ao52426055的博客 1万+

Matlab处理气象数据(六)数据的面积加权

由于地理投影导致导致每个像元实际地面面积不同,越靠近北极实际面积越小,越靠近赤道实际面积越大,如果不进行面积加权就简单平均,会导致温度较实际温度偏低。 用以下公式计算地理投影每个像元的面积: double pi = 3.1415926; double R = 6371007.181; //units - m double area = (pi/180.0)*R*R*fabs(sin(dLat1/180.0*pi) - sin(dLat2/180.0*pi)) * fabs(dLon1 - dLo

设计の诗 1万+

LeetCode Array 121 122 Best Time to Buy and Sell Stock I II

Array 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.,...

Yi Jiao's Blog 159

C#实现跨域访问共享文件夹的账号密码验证方案

本文详细介绍了在C#中实现跨域访问共享文件夹的账号密码验证方案。通过调用Windows API LogonUser函数,结合LOGON32_LOGON_NEW_CREDENTIALS登录类型,可安全模拟指定用户身份,有效解决工作组与域环境间的文件共享访问难题。文章提供了完整的工具类封装、多种实战场景示例及详细的错误排查指南。

pea55的博客 547

使用 DEM 模拟金属烧结过程

了解离散元素法 (DEM) 如何彻底改变烧结工艺,提高制造业的效率和精度。

jkh920184196的博客 1142

Leetcode array 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., buy one and sell one share of the stock),...

owenfy的博客 147

手把手教你用树莓派蓝牙抓取小米温湿度计2代数据,接入HomeAssistant(附密钥获取避坑指南)

本文详细介绍了如何通过树莓派蓝牙抓取小米温湿度计2代数据并接入HomeAssistant的完整流程。重点解析了获取32位加密密钥的三种方法,包括使用Telink Flasher工具、米家APP抓包和开源工具ATC_MiThermometer,并提供了HomeAssistant集成配置和常见问题排查指南,帮助用户实现稳定可靠的温湿度监测。

一名菜鸟攻城师 800

LeetCode Array Easy121. Best Time to Buy and Sell Stock

Description 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 ...

baobo4566的博客 119

Winform从入门到精通(2)——Button(史上最全)

本文介绍winform中Button控件的详细用法,包括所有属性和事件

qq_34059233的博客 1万+

用 ABAP 驾驭 OAuth 2.0:从 Authorization Code 到 SAML Bearer 的全栈落地指南

ABAP平台OAuth 2.0客户端开发实践 本文为SAP开发者详细解析ABAP平台内置OAuth 2.0客户端的使用方法,涵盖四种授权流的最佳实践: 授权流选择: Authorization Code:适用于用户交互场景 SAML 2.0 Bearer:适合已有SAML体系的企业 Client Credentials:系统间通信 Password Credentials:过渡方案(不推荐) 配置流程: 通过SE80定义Client Profile 使用SOAUTH2_CLIENT/OA2C_CONFIG配

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 315

LeetCode C++ 121. Best Time to Buy and Sell Stock【Array/Dynamic Programming】简单

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 th

memcpy0的博客 566

PSINS工具箱学习(四)捷联惯导更新算法

原始 Markdown文档、Visio流程图、XMind思维导图见:https://github.com/LiZhengXiao99/Navigation-Learning。

实践出真理,接毕设/课设项目开发指导 611

LeetCode-Array-121. Best Time to Buy and Sell Stock

问题:https://leetcode.com/problems/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 mos

Renzhouzhou的博客 229

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Leetcode_1_Array_121_Best Time to Buy and Sell Stock

好气哦,我这方法总是会有一个实例通过不了,就是特别长的那个实例,paste如下,欢迎大家找到错误并在留言评论~~~ My wrong code: class Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :rtype: int ...

m0_37976836的博客 155

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

LeetCode_Array_121. Best Time to Buy and Sell Stock买卖股票的最佳时机(C++)

目录 1,题目描述 英文描述 中文描述 2,解题思路 基本思想 3,AC代码 4,解题过程 第一博 第二搏 1,题目描述 英文描述 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 on

&再见萤火虫&的博客 289

leetcode array|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., buy one and sell one share of the stock),...

xueying_2017的博客 202

LeetCode-Array-121-E: 买卖股票的最佳时机I(Best Time to Buy and Sell Stock I)

文章目录思路解法1解法2wait 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。注意你不能在买入股票前卖出股票。 示例 1: 输入: [7,1,5,3,6,4] 输出: 5 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = ...

Heorin X的博客 217

LeetCode|Array|274. H-Index 121. Best Time to Buy and Sell Stock 380. Insert Delete GetRandom O(1)

O(n)的提示:可以找到循环到i为止的最小值,而nums[i]-最小值一定是目前这个i所能达到的最大profit。其次这道题的要点在于用了map可以解决O(1)插入删除,但getrandom需要在vector里进行,每次map变动vector也要变动过,而vector没有快速的删除。解决办法:map的val用来储存在vec中的位置。注意一个细节:实现remove的时候,vec.back()在map里的val要变(位置变化)sort一下然后O(nlogn)复杂度的方法挺好写的。首先注意rand()的应用。

amytheace的博客 71

算法面试必刷:数组旋转、股票买卖与链表操作详解

数组旋转、动态规划和链表操作是算法面试中的核心考点。数组旋转问题通常考察原地操作能力,通过三次反转法可以在O(n)时间复杂度和O(1)空间复杂度下实现。动态规划是解决最优化问题的利器,股票买卖问题通过维护最小价格和最大利润两个变量,展现了动态规划思想的简化应用。链表操作中的双指针技巧能高效解决节点删除问题,快慢指针的配合使用是常见模式。掌握这些基础算法不仅能提升面试通过率,更能培养解决实际工程问题的思维能力。本文以189题数组旋转、121题股票买卖和19题链表删除为例,深入解析高频面试题的解题模板与优化技巧

weixin_33861800的博客 360

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的博客 446

【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

题目 难度 简单 You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximu

Eistert 299

LeetCode3道股票题

121. Best Time to Buy and Sell Stock 题目:买操作必须在卖操作之前,最多交易一次。得到最大利润 关键词: 动态规划, array 思路: 动态规划初始值和动态变量(不需要数组,因为只需要最大profit值,和求子数组最大max一样)。 因为只是交易一次,只要确定谁是卖价和买价。初始买价格,判断prices[i] - buy 是否比存的profit大。如果是,更新profit。判断prices[i]和buy谁大,如果新的prices[i]比存的buy小,更新buy。 解

yuki_bird的博客 247

leetcode-121. Best Time to Buy and Sell Stock

问题描述 假设给定一个数组,第i个元素表示第i天股票的价格。 假如只允许您完成一笔交易(买和卖),则设计出一种算法找到最大利润。 注意不能在买之前就卖了。 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

青菜白玉堂的博客 208

LeetCode Array 123 188 Best Time to Buy and Sell Stock III IV

123. 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 day i. Design an algorithm to find the maximum profit. You may complete at mos...

Yi Jiao's Blog 205

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 (ie, buy one and sell one share of the

编程小白小卿的博客 1399
上一篇: [LeetCode][Array] 55. Jump Game
下一篇: [LeetCode][Array] 11. Container With Most Water
Shlyan
博客等级 码龄9年 0粉丝 98原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值