做博客网站赚钱吗,电子商务网站的设计要素,网站IcP在哪查,温州网站推广公司了解股票的都知道#xff0c;只需要选择股票最低价格那天购入#xff0c;在股票价格与最低价差值最大时卖出即可获取最大收益#xff0c;总之本题只需要维护两个变量即可#xff0c;minPrice和maxProfit#xff0c;收益 prices[i] - minPrice,直接用代码描述如下
class …
了解股票的都知道只需要选择股票最低价格那天购入在股票价格与最低价差值最大时卖出即可获取最大收益总之本题只需要维护两个变量即可minPrice和maxProfit收益 prices[i] - minPrice,直接用代码描述如下
class Solution {public int maxProfit(int[] prices) {//股票最低价格int minPrice Integer.MAX_VALUE;//最大收益int maxProfit 0;for (int i 0; i prices.length; i) {// 计算在最低价那天购入至今天能获取的收益int currentProfit prices[i] - minPrice;// 如果获取的收益小于零说明今天的股票价格更低if (currentProfit 0) {// 把第i天作为股票最低价的那天minPrice prices[i];} else if (currentProfit maxProfit) { // 维护一个最大收益变量maxProfit currentProfit;}}return maxProfit;}
}
题目链接题单 - 力扣LeetCode全球极客挚爱的技术成长平台