当前位置:网站首页>leetcode 121 Best Time to Buy and Sell Stock 买卖股票的最佳时机(简单)
leetcode 121 Best Time to Buy and Sell Stock 买卖股票的最佳时机(简单)
2022-07-04 03:52:00 【InfoQ】
一、题目大意
- 1 <= prices.length <= 105
- 0 <= prices[i] <= 104
二、解题思路
三、解题方法
3.1 Java实现
public class Solution {
public int maxProfit(int[] prices) {
// 记录i位置之前所有价格中的最低价格
int buy = prices[0];
// 当前最大利润
int profit = 0;
for (int i = 1; i < prices.length; i++) {
buy = Math.min(buy, prices[i]);
profit = Math.max(profit, prices[i] - buy);
}
return profit;
}
}
四、总结小记
- 2022/7/3 接下来几天都有雨
边栏推荐
- Graduation project: design seckill e-commerce system
- Spa in SDP
- Epidemic strikes -- Thinking about telecommuting | community essay solicitation
- How was my life in 2021
- Interpretation of leveldb source code skiplist
- Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
- 毕业三年,远程半年 | 社区征文
- 深度优先搜索简要讲解(附带基础题)
- Brief explanation of depth first search (with basic questions)
- C语言双向链表初版
猜你喜欢
PostgreSQL users cannot create table configurations by themselves
Understand the principle of bytecode enhancement technology through the jvm-sandbox source code
Flink学习6:编程模型
[Logitech] m720
I Build a simple microservice project
Parameterization of controls in katalon
ctf-pikachu-XSS
The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
随机推荐
idea修改主体颜色
Leetcode skimming: binary tree 08 (maximum depth of n-ary tree)
[microservice openfeign] use openfeign to remotely call the file upload interface
SDP中的SPA
Restore the subtlety of window position
10 reasons for not choosing to use free virtual hosts
How to add custom API objects in kubernetes (1)
Unity 绘制弹球和台球的运动轨迹
vim正确加区间注释
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
RHCSA 03 - 文件的基础权限
leetcode刷题:二叉树07(二叉树的最大深度)
DP83848+网线热拔插
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
Pytest multi process / multi thread execution test case
Katalon framework test web (XXVI) automatic email
What does software testing do? Find defects and improve the quality of software
My opinion on how to effectively telecommute | community essay solicitation
leetcode刷题:二叉树06(对称二叉树)
【微服务|openfeign】@FeignClient详解