feat: 2025-10-31打卡
This commit is contained in:
22
121.best-time-to-buy-and-sell-stock.java
Normal file
22
121.best-time-to-buy-and-sell-stock.java
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* @lc app=leetcode id=121 lang=java
|
||||
*
|
||||
* [121] Best Time to Buy and Sell Stock
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int maxProfit(int[] prices) {
|
||||
if(prices == null || prices.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int min = prices[0], profit = 0;
|
||||
for(int i=1; i<prices.length; i++) {
|
||||
profit = Math.max(profit, prices[i] - min);
|
||||
min = Math.min(min, prices[i]);
|
||||
}
|
||||
return profit;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
Reference in New Issue
Block a user