JavaScript solution with explanation and comments
This problem can be solved using a sliding window/two-pointer technique. The reason that works is that we\'ve got an array-based problem statement, and we want 
This problem can be solved using a sliding window/two-pointer technique. The reason that works is that we\'ve got an array-based problem statement, and we want to calculate information about certain sub-sections of the array. A fast way to do this, in linear time, is to use two pointers that move through the set of data.\n\nWe\'ll track a few things:\n\n1. We\'ll track a maximum profit. That will start at 0, so we can have a good default value in case there are no profits available (in the case that the stock price only goes down)\n1. We\'ll track a left pointer. The left pointer will start at the beginning of the array.\n1. We\'ll track a right pointer. The right pointer will start at the beginning of the array + 1 - just after the left pointer.\n\nAt each step, we\'ll subtract the value at the left pointer position from the right pointer position. If that value is greater than the current max, we\'ll track it as the new max. In the case where the value is greater than the current max, we\'ll increment the right pointer by one to see if there\'s a better profit available. The left pointer will stay in the same place.\n\nIf the value isn\'t greater than the max, but is positive, we\'ll still move the right pointer, just to see if there are better days in the future to sell.\n\nIf, when we subtract the left pointer from the right pointer, there\'s a negative value, it means that the value at the left can\'t be the minimum price at all. In fact, it means that the value at the right pointer is our new lowest. So we update the left pointer to the right pointer position, and increment the right pointer again.\n\nWe\'ll repeat this until the right pointer has exceeded the length of the array, at which point, we return the max.\n\nHere\'s how to write that in JavaScript:\n\n```js\n/**\n * @param {number[]} prices\n * @return {number}\n */\nvar maxProfit = function(prices) {\n    // Set up variables to track the max profit we\'ve seen (init at 0),\n    // the left pointer (init at day 0)\n    // and the right pointer (init at day 1, since we can\'t buy/sell on the same day)\n    let max = 0\n    let left = 0\n    let right = 1\n    \n    // Keep incrementing the right pointer until we hit the end of the prices array.\n    while (right < prices.length) {\n\n        // The profit on any given day is the difference between the sell date (represented by right pointer)\n        // and buy date (represented by the left pointer)\n        const profit = prices[right] - prices[left]\n        \n        // If we can get a profit higher than we\'ve seen before, set it as our maximum,\n        // and we\'ll keep going.\n        if (profit > max) {\n            max = profit\n        }\n        \n        // If today would be a loss, it means two things: \n        // 1. We don\'t have a new maximum.\n        // 2. We just found a day on which we could buy at a lower price than before, so we should buy now.\n        // \n        // The implication of point two is that we move our left pointer (again, as a sliding window) to be the current day,\n        // as represented by the right pointer.\n        if (profit < 0) {\n            left = right\n        }\n        \n        // Increment the right pointer to keep searching.\n        right += 1\n    }\n    \n    // This will return 0 if we had no possible profit, since we initialized at 0,\n    // or the maximum profit possible.\n    return max\n};\n```\n
['JavaScript']