当前位置:网站首页>Leetcode 121 best time to buy and sell stock (simple)
Leetcode 121 best time to buy and sell stock (simple)
2022-07-04 04:26:00 【InfoQ】
One 、 The main idea of the topic
- 1 <= prices.length <= 105
- 0 <= prices[i] <= 104
Two 、 Their thinking
3、 ... and 、 How to solve the problem
3.1 Java Realization
public class Solution {
public int maxProfit(int[] prices) {
// Record i The lowest of all prices before the location
int buy = prices[0];
// The biggest profit at the moment
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;
}
}
Four 、 Summary notes
- 2022/7/3 It will rain for the next few days
边栏推荐
猜你喜欢
Flink learning 6: programming model
Architecture training graduation design + summary
资深开发人员告诉你,怎样编写出优秀的代码?
Main applications of TDK lambda power supply
【微服务|openfeign】@FeignClient详解
Virtual commodity account trading platform source code_ Support personal QR code collection
Leetcode skimming: binary tree 04 (sequence traversal of binary tree)
Modstartblog modern personal blog system v5.2.0 source code download
dried food! Generation of rare samples based on GaN
统计遗传学:第三章,群体遗传
随机推荐
“找工作不要太在意工资”,这是我听过最大的谎言
多位科技公司创始人向Entrepreneur First提供高达1.58亿美元的C轮融资,协助其投资下一代全球创新者
(pointer) write a function to compare the size of strings by yourself, which is similar to StrCmp.
dried food! Generation of rare samples based on GaN
RHCSA 07 - 用户与群组管理
【云原生】那些看起来很牛X,原理却很简单的一行代码
Flink learning 8: data consistency
Parameterization of controls in katalon
Pytest基础自学系列(一)
Touch your hand and bring you a commonjs specification
[webrtc] M98 Ninja build and compile instructions
【愚公系列】2022年7月 Go教学课程 001-Go语言前提简介
Architecture training graduation design + summary
Programmers' telecommuting is mixed | community essay solicitation
Leetcode brush question: binary tree 06 (symmetric binary tree)
Redis cluster view the slots of each node
什么是上下文?
Interpretation of leveldb source code skiplist
VIM add interval annotation correctly
tdk-lambda电源主要应用