package DP;import java.util.Scanner;/*** @author dx* @version 1.0* @date 2022年5月16日 9:21* @description: 力扣121 买卖股票的最佳时机*/public class MaxProfit {public static void main(String[] args) {Scanner sr = new Scanner(System.in);String [] str = sr.nextLine().split(" ");int [] price = new int[str.length];for(int i =0;i < str.length;i++){price[i] = Integer.parseInt(str[i]);}System.out.println(new MaxProfit_Solution().maxProfit(price));}}class MaxProfit_Solution{public int maxProfit(int [] price){int max_profit = Integer.MIN_VALUE;for(int i =0;i < price.length;i++){int buy = price[i];for(int j = i+1;j < price.length; j++){max_profit = Math.max(max_profit, price[j] - buy);}}if(max_profit <=0){max_profit = 0;}return max_profit;}public int maxProfit2(int [] price){int minprice = Integer.MAX_VALUE;int maxprofit = 0;for(int i = 0;i<price.length;i++){if(price[i] < minprice){minprice = price[i];}else if(price[i] - minprice > maxprofit){maxprofit = price[i] - minprice;}}return maxprofit;}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。