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

IsaacLab从入门到精通(二) 导入自定义机器人 IsaacLab系列第二讲,导入自定义的机器人及设置 阅读详情

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

Problem 121: Best Time to Buy and Sell Stock [Easy]

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.

Example 2:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

来源:LeetCode

解题思路

本题只需遍历一遍数组,维护一个当前最小价格变量和一个当前最大利润变量。由于每个价格都只能与其之前的价格产生利润,因此只需要在遍历过程中不断更新最小价格,并计算当前价格与其的差值是否能够产生更大的收益。

具体过程:

  • 创建minp变量和maxP变量。
  • 遍历数组,更新最小价格minp和最大利润maxP,公式为:
    m i n p = min ⁡ { m i n p , p r i c e s [ i ] } m a x P = max ⁡ { m a x P , p r i c e s [ i ] − m i n p } minp = \min\{minp, prices[i]\}\\ maxP = \max\{maxP, prices[i] - minp\} minp=min{minp,prices[i]}maxP=max{maxP,prices[i]minp}
    其中 p r i c e s [ i ] prices[i] prices[i]为当前价格。
  • 最后返回最大利润值maxP。

时间复杂度为 O ( n ) O(n) O(n),空间复杂度为 O ( 1 ) O(1) O(1)

运行结果:
在这里插入图片描述

Solution (Java)

class Solution {
    public int maxProfit(int[] prices) {
        int N = prices.length;
        if(N < 2) return 0;
        int minp = prices[0];
        int maxP = 0;
        for(int i = 1; i < N; i++){
            minp = Math.min(minp, prices[i]);
            maxP = Math.max(maxP, prices[i] - minp);
        }
        return maxP;
    }
}
IEEE投稿完整流程——以IEEE Transactions on Cybernetics为例【附:论文格式模板】 前言 以下是官方的流程,包括了论文的格式和提交的流程,重要!重要!重要! 在IEEE的官网中,也有论文提交的文件 可以直接参考: 中文版: 作者指南 英文版: Manuscripts-Author-Guide 作者指南的版本指导流程可能和最新版的官网不一致!!! 或者在下面的链接中,按照你的论文类型,选择不同的论文模板: IEEE article template 以下以IEEE Transactions on Cybernetics 为例子 IEEE Transactions on Cyberne 阅读详情

相关推荐

工业相机,大恒,面振相机8脚电源线和I/O触发接口线,颜色和接法说明和触发软件设定

上图: 说明一下,工业相机有TTL逻辑电平触发,也有光耦触发等。 这里一般我们用电路板驱动,就用GPIO触发模式, 这里,准备了两个GPIO触发模式,为LINE2,LINE3都可以选择,然后有一个GPIO_GND 如果,你只有一个触发接口,那么如图,选两个就好了。 实际接法如图。 ...

山云的专栏 1万+

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

Problem Say you have an array for which the ithi^{th}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 shar...

DaVinciL的博客 509

Android 12.0 framework系统修改安兔兔等显示的屏幕尺寸大小功能实现

在12.0的系统rom定制化开发中,在使用第三方app检测系统的一些信息中,比如安兔兔 设备信息等检测app中,有时候显示的屏幕尺寸大小和 产品规格书等信息不同,稍微有些差异,所以就需要看下系统framework层中,相关的设备信息是怎么读出来的,然后做些调整 接下来就来分析相关功能

安卓兼职framework和app工程师的博客 462

LeetCode 121: Best Time to Buy and Sell Stock 解题与思考

LeetCode 121: Best Time to Buy and Sell Stock 解题与思考[原题链接]题目描述给定一个数列{ana_n},其中aia_i 为第i天的股票价格, 假设你在这些天只能选择买入卖出一次,求最大利润。思路自然我们不能简单地通过寻找最大最小值来确定哪天买入卖出,因为这样可能高开低走,得到错误结果。不过我们可以在顺序扫描数组时,维护扫描过的数的最小值以及扫描过的数中的

码农a嘘的博客 263

leetcode解题报告:121 Best Time to Buy and Sell Stock

问题:给定一个列表,第i个元素代表股票第i天的价值,只允许买入卖出一次,求最大收益思路:动态规划开始没想清楚,觉着遍历一次找出最大与最小股价求差就是,后来发现脑子绣住了,买入要在买出之前。输入为列表p1p2...pmm[i]表示前i天的最小股价,m[i] = min(p[i], m[i-1]) i >= 1 <-- O(n)时间开销第i天卖出的最大收益是pi - ...

weixin_34174322的博客 85

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

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

聚沙成塔 482

Leetcode Best Time to Buy and Sell Stock python 问题解析

Leetcode 121Best 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....

润泽的博客 284

leetcode 123. Best Time to Buy and Sell Stock III

/二是第i天买入了第二只股票(需支付prices[i]),第i天能买入第二只股票的前提是第i-1天处于已经卖出第一只股票但还没买入第二只股票的状态(对应dp[i-1][2])//二是第i天第一次买入股票(需支付prices[i]),第i天能第一次买入股票的前提是前一天(第i-1天)处于没有买过股票的状态(对应状态dp[i-1][0])//二是第i天卖出了第一只股票(收入prices[i]),第i天能卖出第一只股票的前提是第i-1天处于持有第一只股票的状态(对应dp[i-1][1])

weixin_41554427的博客 1374

LeetCodeBest Time to Buy and Sell Stock I II III IV 解题报告

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

跳出温水的青蛙 2万+

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

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

方亮的专栏 1336

LeetCode解题126:Word Ladder II(BFS算法

LeetCode解题 126:Word Ladder II(BFS算法)Problem 126: Word Ladder II [Hard]解题思路Solution (Java)修改过程 Problem 126: Word Ladder II [Hard] Given two words (beginWord and endWord), and a dictionary’s word list, ...

Fayedily的博客 771

LeetCode解题188:Best Time to Buy and Sell Stock IV(动态规划)

LeetCode解题 188:Best Time to Buy and Sell Stock IV (动态规划)Problem 188: Best Time to Buy and Sell Stock IV [Hard]解题思路Solution (Java)修改过程 Problem 188: Best Time to Buy and Sell Stock IV [Hard] Say you hav...

Fayedily的博客 594

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

LeetCode解题119:Pascal's Triangle II

LeetCode解题 118:Pascal's Triangle II Problem 118: Pascal's Triangle II [Easy]解题思路Solution (Java) Problem 118: Pascal’s Triangle II [Easy] Given a non-negative index k where k ≤ 33, return the kth index...

Fayedily的博客 444

LeetCode解题78:Subsets

LeetCode解题 78:Subsets Problem 78: Subsets [Medium]解题思路Solution (Java)修改过程 Problem 78: Subsets [Medium] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The so...

Fayedily的博客 443

LeetCode解题44:Wildcard Matching(动态规划解法)

LeetCode解题 44:Wildcard Matching(动态规划解法)Problem 44: Wildcard Matching [Hard]解题思路Solution (Java) Problem 44: Wildcard Matching [Hard] Given an input string (s) and a pattern (p), implement wildcard patt...

Fayedily的博客 429

LeetCode解题10:Regular Expression Matching(动态规划解法)

LeetCode解题 10:Regular Expression Matching(动态规划解法)Problem 10: Regular Expression Matching [Hard]解题思路Solution (Java) Problem 10: Regular Expression Matching [Hard] Given an input string (s) and a patter...

Fayedily的博客 411

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

LeetCode解题31:Next Permutation

LeetCode解题 31:Next Permutation Problem 31: Next Permutation [Medium]解题思路Solution (Java)修改过程 Problem 31: Next Permutation [Medium] Implement next permutation, which rearranges numbers into the lexicogr...

Fayedily的博客 352

LeetCode解题32:Longest Valid Parentheses(多解法:栈+动态规划+计数器)

LeetCode解题 32:Longest Valid Parentheses(多解法:栈+动态规划+计数器)Problem 32: Longest Valid Parentheses [Hard]解题思路1. 栈2. 动态规划3. 计数器Solution (Java) Problem 32: Longest Valid Parentheses [Hard] Given a string cont...

Fayedily的博客 352

LeetCode解题714:Best Time to Buy and Sell Stock with Transaction Fee(动态规划)

LeetCode解题 714:Best Time to Buy and Sell Stock with Transaction Fee (动态规划)Problem 714: Best Time to Buy and Sell Stock with Transaction Fee [Medium]解题思路Solution (Java) Problem 714: Best Time to Buy an...

Fayedily的博客 339

FX3U-2HC高速计数模块使用手册(中文)

FX3U-2HC型高速计数器模块(以下称为FX3U-2HC)内置2通道高速计数器,作为FX3U、FX3UC系列可编程控制器的特殊功能模块使用。

《企业卓越中心(CoE)-先进制造业行业专知合作生态的最佳实践》电子书

在数字化转型的浪潮中,企业如何实现知识共享、资源整合与持续改进?《企业卓越中心(CoE)——先进制造业行业专知合作生态的最佳实践》为您揭开CoE(Center of Excellence)的神秘面纱,助力企业迈向卓越。本电子书深入解读了企业CoE的定义、优点、目的、构建领域、以及如何建立等,为理解CoE提供了清晰的路径。体系化构建路径:完整定义解析:PMI、Gartner、TechTarget三大权威机构对CoE的差异化诠释;四阶段实施蓝图:Crawl(探索)- Walk(验证)- Jog(优化)- Run(卓越)的进阶模型;双目标驱动:持续改进型CoE vs 商业发展型CoE的运作差异。实操工具包:团队组建策略对比表:内部培养、新聘员工、外部顾问的优劣势分析;跨部门协作框架:与实践社区(CoP, Community of Practice)、工作团队的联动机制;7大建设领域:从云迁移到垂直领域专精的部署指南。行业专属方案:先进制造业行业专知合作生态(AICoE, Advanced Industries CoE)构建:航空、医疗、汽车等先进制造领域的知识共享机制;工业知识库(xPro)建设:结构化、可交易的行业专知管理方案。《企业卓越中心(CoE)》电子书,为您揭示先进制造业行业专知合作生态的实践之路:深入了解CoE的定义与优点;洞悉何时需要建立CoE;掌握构建CoE的完整路径;探索先进制造业的专属CoE——AICoE。

上一篇: 【LeetCode】解题120:Triangle
下一篇: 【LeetCode】解题122:Best Time to Buy and Sell Stock II
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值