O(n)time/BEATS 99.97% MEMORY/SPEED 0ms MAY 2022
As a professional with a passion for problem-solving and collaboration, I am always looking to expand my network of like-minded individuals on LinkedIn. By conn
**As a professional with a passion for problem-solving and collaboration, I am always looking to expand my network of like-minded individuals on LinkedIn. By connecting with me, we can work together to tackle complex challenges, share ideas, and grow both professionally and personally.**\n\n**Whether you\'re an expert in your field or just starting out, I welcome connections from all backgrounds and experiences. By building a diverse and collaborative network, we can leverage our unique perspectives and skill sets to push the boundaries of what\'s possible.**\n\n**So, if you\'re interested in connecting and exploring the potential for future collaborations, please don\'t hesitate to reach out. Let\'s start a conversation and see where it takes us!**\n\n---\n\nhttps://www.linkedin.com/in/cucerdariancatalin/\nhttps://www.linkedin.com/in/cucerdariancatalin/\nhttps://www.linkedin.com/in/cucerdariancatalin/\n\n---\n\n```\n```\n\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, ***please upvote*** this post.)\n***Take care brother, peace, love!***\n\n```\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 38.2MB*** (beats 92.04% / 24.00%).\n* ***Java***\n```\nclass Solution {\n    public int maxProfit(int[] prices) {\n        \n        int max=0,min=prices[0];\n        for(int i=1;i<prices.length;i++)   {\n\t\t\n            if(min<prices[i])\n                max=Math.max(prices[i]-min,max);\n            else\n                min=prices[i];\n        }\n        return max;\n    }\n}\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 7.0MB*** (beats 100.00% / 100.00%).\n* ***C++***\n```\nint maxProfit(vector<int>& prices) {\n        int n= prices.size();\n        int maxprofit = 0;\n        int minvalue = INT_MAX;\n        for(int i =0;i<n;i++){\n            minvalue = min(prices[i],minvalue);\n            maxprofit= max(maxprofit,prices[i] - minvalue);\n        }\n        return maxprofit;\n    }\n```\n\n```\n```\n\n```\n```\n\n\nThe best result for the code below is ***26ms / 12.2MB*** (beats 95.42% / 82.32%).\n* ***Python***\n```\nclass Solution:\n    def maxProfit(self,prices):\n        left = 0 #Buy\n        right = 1 #Sell\n        max_profit = 0\n        while right < len(prices):\n            currentProfit = prices[right] - prices[left] #our current Profit\n            if prices[left] < prices[right]:\n                max_profit =max(currentProfit,max_profit)\n            else:\n                left = right\n            right += 1\n        return max_profit\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***51ms / 34.2MB*** (beats 100.00% / 84.12%).\n* ***JavaScript***\n```\nconst maxProfit = (prices) => {\n  let left = 0; // Buy\n  let right = 1; // sell\n  let max_profit = 0;\n  while (right < prices.length) {\n    if (prices[left] < prices[right]) {\n      let profit = prices[right] - prices[left]; // our current profit\n\n      max_profit = Math.max(max_profit, profit);\n    } else {\n      left = right;\n    }\n    right++;\n  }\n  return max_profit;\n};\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***68ms / 44.2MB*** (beats 100.00% / 45.25%).\n* ***Kotlin***\n```\nfun maxProfit(prices: IntArray): Int {\n   prices.foldIndexed<Int>(prices[0]) { i, min, n ->\n\t\tprices[i] = n - min\n\t\tif (n < min) n else min\n   }\n   return prices.max() ?: 0\n} \n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***12ms / 32.2MB*** (beats 95% / 84%).\n* ***Swift***\n```\nfunc maxProfit(_ prices: [Int]) -> Int {\n    guard prices.count > 1 else { return 0 }\n    var best = 0, current = 0\n    for index in 1..<prices.endIndex {\n        current = max(0, current + prices[index] - prices[index-1])\n        best = max(best, current)\n    }\n    return best\n}\n```\n\n```\n```\n\n```\n```\n\n***"Open your eyes. Expect us." - \uD835\uDCD0\uD835\uDCF7\uD835\uDCF8\uD835\uDCF7\uD835\uDD02\uD835\uDCF6\uD835\uDCF8\uD835\uDCFE\uD835\uDCFC***
['Swift', 'C', 'Python', 'C++', 'Java', 'Python3', 'Kotlin', 'JavaScript']