Given an array of stock prices for a day, where prices[i] is the price of the stock at time i, write a function that returns the maximum amount of money one could make in a day if he buys and sells at most once. Input: [4,6,2,8,1,2] Output: 6 Input: [6,5,2,10,11,1,1,1] Output: 9 ```javascript // The trash work // O(n^2) b/c you compare every combination. const (prices) => { let buyPrice = 0; let sellPrice = 0; let maxProfit = 0; for(let i = 0; i < prices.length - 1; i++) { buyPrice = prices[i]; for (let j = i + 1; j < prices.length; j++) { sellPrice = prices[j] newDiff = sellPrice - buyPrice if ((newDiff) > maxProfit) { maxProfit = newDiff; } } } return maxProfit; } /** *Given an array of stock prices for a day, where prices[i] is the price of the stock at time i, write a function that returns the maximum amount of money one could make in a day if he buys and sells at most once. @param prices is a non-empty array of at least two numbers */ // Example Input: [6,5,2,10,11,1,1,1] // Output: 9 // O(n) b/c only looping once -- making comparisons as you go const maxProfit = (prices) => { // Initialise vars let minPrice = prices[0]; // First item is max // let maxProf = -1; // Assumes prices of non-negative int let maxProf = prices[1] - prices[0]; // For each price after initial, // If curr price lower than minPrice, set as new min for (let i = 1; i < prices.length - 1; i++) { if (prices[i] < minPrice) minPrice = prices[i] // Compare price w/ curr minPrice let diff = prices[i] - minPrice // if ((prices[i+1] - minPrice) > maxProf) maxProf = diff // If greater profit than curr maxProf, replace it if (diff > maxProf) maxProf = diff } // Return current maxProf return maxProf; }