import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param prices int整型一维数组 * @return int整型 */ public int max_profit (int[] prices) { int maxProfit = 0; int min = prices[0]; for (int i = 1; i < prices.length; i++) { min = Math.min(min,prices[i]); maxProfit = Math.max(maxProfit,prices[i]-min); } return maxProfit; } }
本题知识点分析:
1.贪心算法
2.数组遍历
3.API函数(Math.min和Math.max)
本题解题思路分析:
1.一边用Math.min遍历找数组中最小买入的点
2.一边用Math.max找后面减取最小买入点能获得的最大利润
3.本题=力扣买卖股票最佳时机I