Array - 121. Best Time to Buy and Sell Stock
时间:2022-07-25
本文章向大家介绍Array - 121. Best Time to Buy and Sell Stock,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
Say you have an array for which the _i_th 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.
思路:
这一题使用dp来做,实质也就是找到当前价之前的最低价,来判断盈利,也可以转换为求最大子数组和(53题)来做,也可以用贪心,直接找出最低价与当前盈利。
代码:
java:
class Solution {
// 1. time:O(n) space:O(0)
/*public int maxProfit(int[] prices) {
int len = prices.length;
if (len < 1) return 0;
int[] minPrices = new int[len];
int[] maxProfit = new int[len];
minPrices[0] = prices[0];
maxProfit[0] = 0;
for (int i = 1; i < len; i++) {
minPrices[i] = Math.min(minPrices[i-1], prices[i]);
maxProfit[i] = Math.max(maxProfit[i-1], prices[i] - minPrices[i-1]);
}
return maxProfit[len-1];
}*/
// 2. time: O(n) space:O(1)
public int maxProfit(int[] prices) {
int len = prices.length;
if (len < 1) return 0;
int profit = 0;
int buyPrice = prices[0];
for (int i = 1; i < len; i++) {
buyPrice = Math.min(prices[i], buyPrice);
profit = Math.max(prices[i] - buyPrice, profit);
}
return profit;
}
}
- MySQL InnoDB Lock(一)
- Java 时间类-Calendar、Date、LocalDate/LocalTime
- Java消息队列--JMS概述
- Java FtpClient 实现文件上传服务
- Java消息队列--ActiveMq 实战
- Java消息队列-Spring整合ActiveMq
- 【知识】SAS数据分析完整笔记(3)
- 深入浅出Redis-Spring整合Redis
- Stream-快速入门Stream编程
- MySQL Regular Expression
- Jenkin-持续集成
- 4.3.4.7 Pattern Matching
- mysql left join、right join、inner join用法分析
- _CrtSetDbgFlag
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- (七)golang--变量之基本数据类型(看这篇就够了)
- Mybatis学习笔记(五)Mybatis中已经显示数据已修改但数据库中记录未更新问题
- 【自然语言处理(一)】相关基础技能
- MySQL innoDB的事务隔离
- 多元线性回归模型
- (八)golang--复杂类型之指针
- 关于HttpPost 请求和PostMan请求访问出错
- Proxy与Reflect学习笔记
- 确定的有穷状态机(DFA) -- 你来看也能懂的C++代码示例
- python中的any和all函数
- 关于数据库中NOT NUll 的问题。
- 通俗点聊聊算法 -- 链表误成环
- (十)golang--运算符
- (十一)golang--键盘输入
- python中的__call__和__repr__魔术方法