C# - One Pass Approach with Explanation
Intuition: \nTo maximize the profit, we iterate through the array while keeping track of the minimum price encountered so far. We calculate the potential profit
# Intuition: \nTo maximize the profit, we iterate through the array while keeping track of the minimum price encountered so far. We calculate the potential profit by selling at each price if we had bought at the minimum price. We update the maximum profit whenever a higher profit is obtained.\n\n# Approach:\n- Initialize `minPrice` as the maximum possible value and `maxProfit` as 0.\n- Iterate through the `prices` array.\n- For each price, check if it is lower than the current `minPrice`. If so, update `minPrice`.\n- Calculate the profit by subtracting `minPrice` from the current price.\n- If the profit is higher than the current `maxProfit`, update `maxProfit`.\n- Return the final `maxProfit` value.\n\n# Complexity:\n- Time complexity: O(n), where n is the length of the `prices` array. We iterate through the array once.\n- Space complexity: O(1). We use a constant amount of extra space to store variables.\n\n\n# Code\n```\npublic class Solution {\n    public int MaxProfit(int[] prices) {\n        int minPrice = int.MaxValue;\n        int maxProfit = 0;\n        \n        for (int i = 0; i < prices.Length; i++) {\n            if (prices[i] < minPrice) {\n                minPrice = prices[i];\n            }\n            \n            int profit = prices[i] - minPrice;\n            if (profit > maxProfit) {\n                maxProfit = profit;\n            }\n        }\n        \n        return maxProfit;\n    }\n}\n\n```
['C#']