[Java/C++] best-ever EXPLANATION could possible
Let\'s understand this problem by an imagination. Imagine you have given a time machine, you can go to past to buy the stock of your choice when the price is ve
Let\'s understand this problem by an **imagination**. **Imagine** you have given a **time machine**, you can go to **past** to **buy** the **stock** of your choice when the price is very least. And again using that **time machine** you went into **future** to **sell** the **stock**.\n\n![image](https://assets.leetcode.com/users/images/21002ba2-d686-4465-8486-38e3ccd4a063_1643683166.0503008.png)\n\nBy doing that you have achieve **maximum profit**. From `buying at very least price and selling at very higher price`. **And you have become rich now!**\n\n![image](https://assets.leetcode.com/users/images/2950d9ce-0000-49dd-8270-cdfbf8a7a6af_1643683545.5999126.png)\n\nNow let\'s just understand it with our given example,\n**Input**: prices = [7,1,5,3,6,4]\n**Output**: 5\n\n![image](https://assets.leetcode.com/users/images/ca7614a0-a717-48c1-a4ba-7fba42046941_1643684561.8969386.png)\n\n`Remember one rule :- You can only buy one time & sell one time`\n\n* So, if **buy at 7** & **sell at any time in the future**, we\'ll face loss. Because **buying price** is way **higher** then **selling price** available we have\n* Now, I have seen a dip & I **buy at 1** & **sell at 5** my **overall profit** will be **5 - 1 = 4**\n\n* But what if, I had **buy at 1** & **sell at 6** my profit will be **6 - 1 = 5**. Which is **greater then my overall profit**. So, i will **update** my **overall profit** with new value.\n* Now we have done as further we don\'t have any higher point to sell. We will **return our answer.**\n\n**I hope now question, approach is absolute clear.**\n\n*code each line explained :* `Similar for C++, Java`\n\n```\n{\n        int lsf = Integer.MAX_VALUE; // least so far\n        int op = 0; // overall profit\n        int pist = 0; // profit if sold today\n        \n        for(int i = 0; i < prices.length; i++){\n            if(prices[i] < lsf){ // if we found new buy value which is more smaller then previous one\n                lsf = prices[i]; // update our least so far\n            }\n            pist = prices[i] - lsf; // calculating profit if sold today by, Buy - sell\n            if(op < pist){ // if pist is more then our previous overall profit\n                op = pist; // update overall profit\n            }\n        }\n        return op; // return op \n```\n\n**Java**\n```\nclass Solution {\n    public int maxProfit(int[] prices) {\n        int lsf = Integer.MAX_VALUE;\n        int op = 0;\n        int pist = 0;\n        \n        for(int i = 0; i < prices.length; i++){\n            if(prices[i] < lsf){\n                lsf = prices[i];\n            }\n            pist = prices[i] - lsf;\n            if(op < pist){\n                op = pist;\n            }\n        }\n        return op;\n    }\n}\n```\n**C++**\n```\nclass Solution {\npublic:\n    int maxProfit(vector<int>& prices) {\n        int lsf = INT_MAX;\n        int op = 0;\n        int pist = 0;\n        \n        for(int i = 0; i < prices.size(); i++){\n            if(prices[i] < lsf){\n                lsf = prices[i];\n            }\n            pist = prices[i] - lsf;\n            if(op < pist){\n                op = pist;\n            }\n        }\n        return op;\n    }\n};\n```
['C', 'Java']