买卖股票的最佳时机
很容易就去使用双while循环去暴力求解了。
我觉得,有点像贪心的思路?
var maxProfit = function(prices) {let minPrice = Infinity;let maxProfit = 0for (let i = 0; i < prices.length; i++) { // 假如在第i天卖出,那么要找出之前的最小值if (minPrice > prices[i]) {minPrice = prices[i];} else if (maxProfit < prices[i] - minPrice) {maxProfit = prices[i] - minPrice;}}return maxProfit};
122. 买卖股票的最佳时机 II
虽然标着中等难度,其实非常简单:把所有挣钱的阶段都找出来,然后求和即可。
var maxProfit = function(prices) {let result = [];let profit = 0;let sum = 0;for (let i = 1; i < prices.length; i++) {profit = prices[i] - prices[i - 1];if (profit > 0) {sum += profit}}return sum;};
股票系列 还有很多,但是这篇文章,这个不是重点。
162. 寻找峰值

没有这个提示,或者说是题目要求这道题要复杂很多的。
思路:寻找什么东东,模式匹配 => 二分查找。
