🌺C#, Java, Python3, JavaScript Solution - O(n)
Here to see the full explanation :\u2B50Zyrastory - #121 Best Time to Buy and Sell Stock\u2B50\n\n\n---\n\n# Example :\n# C#\n\u274Cfirst try  - Time Complexity
**Here to see the full explanation :\u2B50[Zyrastory - #121 Best Time to Buy and Sell Stock](https://zyrastory.com/en/coding-en/leetcode-en/leetcode-121-best-time-to-buy-and-sell-stock-solution-and-explanation-en/)\u2B50**\n\n\n---\n\n# Example :\n# C#\n\u274Cfirst try  - **Time Complexity : O(n^2)**\n```\npublic class Solution {\n    public int MaxProfit(int[] prices) {\n        \n        int max = 0;\n         for(int i = 0;i<prices.Length-1;i++)\n         {\n             for(int j = i+1;j<prices.Length;j++)\n             {\n                 int res = prices[j]-prices[i];\n                 if(res>max)\n                 {\n                     max = res;\n                 }\n             }\n         }\n        return max;\n    }\n}\n```\n\nSolution - **Time Complexity : O(n)**\n```\npublic class Solution {\n    public int MaxProfit(int[] prices) {\n        int max = 0;\n        int min = prices[0];\n        \n        for(int i=1;i<prices.Length;i++){\n            if(prices[i] < min){\n                min = prices[i];\n            }\n            \n            else if((prices[i] - min) > max )\n            {\n                max = prices[i] - min;\n            }\n        }\n        return max;\n    }\n}\n```\n\n---\n\n# Java\n```\nclass Solution {\n    public int maxProfit(int[] prices) {\n        int max = 0;\n        int min = prices[0];\n        \n        for(int i=1;i<prices.length;i++){\n            if(prices[i] < min){\n                min = prices[i];\n            }\n            \n            else if((prices[i] - min) > max )\n            {\n                max = prices[i] - min;\n            }\n        }\n        return max;\n    }\n}\n```\n\n---\n# Python3\n```\nclass Solution:\n    def maxProfit(self, prices: List[int]) -> int:\n        max = 0;\n        min = prices[0];\n        \n        for i in range(1,len(prices)):\n            if prices[i] < min :\n                min = prices[i]\n            \n            elif((prices[i] - min) > max):\n                max = prices[i] - min\n        \n        return max;\n```\n\n---\n\n# JavaScript\n```\n/**\n * @param {number[]} prices\n * @return {number}\n */\nvar maxProfit = function(prices) {\n    var max = 0;\n    var min = prices[0];\n\n    for(var i=1;i<prices.length;i++){\n        if(prices[i] < min){\n            min = prices[i];\n        }\n\n        else if((prices[i] - min) > max )\n        {\n            max = prices[i] - min;\n        }\n    }\n    return max;\n};\n```\n\n\n**If you got any problem about the explanation or you need other programming language solution, please feel free to let me know (leave comment or messenger me).**\n\nThanks!\n\n**\uD83E\uDDE1See next question - [Zyrastory - #122 Best Time to Buy and Sell Stock II](https://zyrastory.com/en/coding-en/leetcode-en/leetcode-122-best-time-to-buy-and-sell-stock-ii-solution-and-explanation-en/)**
['Java', 'Python3', 'JavaScript', 'C#']