Leetcode-数据结构-121. 买卖股票的最佳时机
问题:
给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。
设计一个算法来计算你所能获取的最大利润。
返回可以从这笔交易中获取的最大利润,
如果不能获取任何利润,返回 0 。
思路:
我们需要找出给定数组中两个数字之间的最大差值(即,最大利润)★。
此外,第二个数字(卖出价格)必须大于第一个数字(买入价格)。
形式上,对于每组 i 和 j(其中 j > i)
我们需要找出 max(prices[j]−prices[i])。
代码:
main函数
package DataStructure_start;public class DS20230113 {public static void main(String[] args) {int[] prices = {6, 8, 3, 4, 8, 9, 1};int[] prices2 = {7, 1, 5, 3, 6, 4};int maxProfit = maxProfit(prices);int maxProfit2 = maxProfit2(prices2);System.out.println(maxProfit);System.out.println(maxProfit2);}
}
方法一:暴力法
思路:
挨个遍历两遍,找到两个数,计算出最大差值
复杂度分析
时间复杂度:O(n^2),循环运行 n (n-1)/2次。
空间复杂度:O(1)只使用了常数个变量。
public static int maxProfit(int[] prices) {int maxprofit = 0;// 第一遍遍历for (int i = 0; i < prices.length - 1; i++) {
// 紧跟着遍历第二个数for (int j = i + 1; j < prices.length; j++) {// 设置profit为两数的差值int profit = prices[j] - prices[i];
// 如果差值大于“目前的”最大值if (profit > maxprofit) {
// 则将目前的差值赋值给maxfprofitmaxprofit = profit;}}}
// 返回最大差值return maxprofit;}
方法二:一次遍历
我们来假设自己来购买股票。
随着时间的推移,每天我们都可以选择出售股票与否。
那么,假设在第 i 天,如果我们要在今天卖股票,那么我们能赚多少钱呢?
显然,如果我们真的在买卖股票,我们肯定会想:
如果我是在历史最低点买的股票就好了!
太好了,在题目中,我们只要用一个变量记录一个历史最低价格 minprice,我们就可以假设自己的股票是在那天买的。
那么我们在第 i 天卖出股票能得到的利润就是 prices[i] - minprice。
因此,我们只需要遍历价格数组一遍,记录历史最低点,然后在每一天考虑这么一个问题:
如果我是在历史最低点买进的,那么我今天卖出能赚多少钱?
当考虑完所有天数之时,我们就得到了最好的答案。
复杂度分析
时间复杂度:O(n),只需要遍历一次。
空间复杂度:O(1),只使用了常数个变量。
public static int maxProfit2(int prices[]) {int minprice = Integer.MAX_VALUE;int maxprofit = 0;// 遍历一次for (int i = 0; i < prices.length; i++) {
// 将小的值赋值给minprice(找最低点)if (prices[i] < minprice) {minprice = prices[i];
// 如果差值大于maxprofit,则将最大差值赋值给maxprofit} else if (prices[i] - minprice > maxprofit) {maxprofit = prices[i] - minprice;}}return maxprofit;}
补充:
Integer.MAX_VALUE:
public final class Integer extends Number implements Comparable<java.lang.Integer> {*/
/*** A constant holding the minimum value an {@code int} can* have, -2<sup>31</sup>.*//*@Nativepublic static final int MIN_VALUE = 0x80000000;*/
/*** A constant holding the maximum value an {@code int} can* have, 2<sup>31</sup>-1.*//*@Native public static final int MAX_VALUE = 0x7fffffff;*/
/*** The {@code Class} instance representing the primitive type* {@code int}.** @since JDK1.1
参考:
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Leetcode-数据结构-121. 买卖股票的最佳时机相关推荐
- 【LeetCode】121. 买卖股票的最佳时机(C++)
121. 买卖股票的最佳时机 1 题目描述 2 示例描述 2.1 示例1 2.2 示例2 3 解题提示 4 解题思路 5 暴力代码(超时) 6 解答代码 1 题目描述 给定一个数组 prices ,它 ...
- 【LeetCode】121.买卖股票的最佳时机
题目 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. ...
- leetcode系列-121. 买卖股票的最佳时机
题目描述: 给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格.你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票. 设 ...
- 48 - LeetCode 122 121. 买卖股票的最佳时机
// algorigthm 的 min 和 max 函数用于保存当前状态#include <algorithm> #include <vector> using namespa ...
- LeetCode:121.买卖股票的最佳时机——动态规划
- leetcode - 121.买卖股票的最佳时机
121.买卖股票的最佳时机 ------------------------------------------ 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成 ...
- 《LeetCode刷题》—121. 买卖股票的最佳时机
<LeetCode刷题>-121. 买卖股票的最佳时机 一.题目内容 原题连接:https://leetcode.cn/problems/best-time-to-buy-and-sell ...
- LeetCode #121 买卖股票的最佳时机 贪心 单调栈 动态规划
LeetCode #121 买卖股票的最佳时机 题目描述 给定一个数组,它的第 iii 个元素是一支给定股票第 iii 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票一次),设计一个算 ...
- (C++)Leetcode狂刷200题——标签“数组篇--简单难度10道 #121. 买卖股票的最佳时机
第五道题 #121. 买卖股票的最佳时机 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票一次),设计一个算法来计算你所能获取的最大 ...
- LeetCode - 121. 买卖股票的最佳时机
121. 买卖股票的最佳时机 class Solution {public int maxProfit(int[] prices) {if (prices == null || prices.leng ...
最新文章
- Linux_文件系统磁盘分区
- 入围51CTO 2009年度“最受读者欢迎的IT图书作者”评选
- linux写文件优化,Linux文件系统性能优化
- 基于socket的线上聊天框
- 关于Oracle中varchar2和dbms_output.put_line的长度限制问题小结
- Android 一s个相对完整的自动升级功能实现代码
- python调用百度接口实现ocr识别_Python调用百度OCR实现图片文字识别的示例代码
- Python编程从入门到实践~异常
- Java作业-数据库
- prisma graphql 工具基本使用
- html怎么设置自动音乐播放器,运用js教你轻松制作html音乐播放器
- Origin2017、Origin2018详细安装教程
- ADS中功放的PAE计算总结
- 串行口通讯测试软件,485串口测试软件
- 大学生数学竞赛试题荟萃 (更新至2017年10月28日)
- 基于STM32的EtherCAT从站IO讲解
- 浏览器访问网址过程详解
- java基础思维导图(java基础部分)
- soc 设计soc设计 uml实务手册_SoC技术的发展
- 纯js导出Excel文件(无需引入插件)