1-line vs 2 priority_queues vs loop||11 ms Beats 99.06%
Intuition\n Describe your first thoughts on how to solve this problem. \nUse sort 1-line by Python\nC++ sort.\nOther approaches are\n- Heap solution\n- Loop wit
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse sort 1-line by Python\nC++ sort.\nOther approaches are\n- Heap solution\n- Loop with conditional clause\n# Approach\n<!-- Describe your approach to solving the problem. -->\n[https://youtu.be/I2VRYiBjK1k?si=UkDt91Fhu3wAh1K2](https://youtu.be/I2VRYiBjK1k?si=UkDt91Fhu3wAh1K2)\n\nSince the question is easy, try other approach by using priority_queue. Keep the size below 3 for 2 priority_queues with different orderings. \n\nAt final 1 priority_queue contains 2 biggest numbers,  & other priority_queue contains 2 smallest numbers.\n\nThis method is not slow runs in 19ms & beats 83.95%.\n\nA loop with if-else clause is also implemented which runs in 11 ms &beats 99.06%. This is a a standard solution which is explained as follows\n\nVariable Initialization:\n`int max0=1, max1=1, min1=INT_MAX, min0=INT_MAX;`: Initializes four variables (max0, max1, min1, min0) to specific values. max0 and max1 are initialized to 1, and min1 and min0 are initialized to the maximum representable integer value (INT_MAX).\nLoop Over Input Vector:\n\n`for(int x: nums) { ... }`: Iterates over each element x in the nums vector.\nInside the loop, there are conditional statements to update the maximum and minimum values:\nIf x is greater than max0, update max1 and max0 accordingly.\nIf x is less than min0, update min1 and min0 accordingly.\n\n\n`return max0*max1-min0*min1;`: Computes the result, which is the product of the two largest elements (max0 and max1) minus the product of the two smallest elements (min0 and min1), and returns it.\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n\\log n)$$\n2 priority_queues: $O(n)$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n2 priority_queues: $O(n)$\n# Python 1-line Code\n```\nclass Solution:\n    def maxProductDifference(self, nums: List[int]) -> int:\n        return (X:=sorted(nums))[-1]*X[-2]-X[1]*X[0]\n```\n# C++ using sort\n```\nclass Solution {\npublic:\n    int maxProductDifference(vector<int>& nums) {\n        sort(nums.begin(), nums.end());\n        int n=nums.size();\n        return nums[n-1]*nums[n-2]-nums[1]*nums[0];\n        \n    }\n};\n```\n# C++ using 2 priority_queues||19ms Beats 83.95%\n```\nclass Solution {\npublic:\n    int maxProductDifference(vector<int>& nums) {\n        priority_queue<int> pq0;\n        priority_queue<int, vector<int>, greater<int>> pq1;\n        for(int x: nums){\n            pq0.push(x);\n            if (pq0.size()>2){\n                int y=pq0.top();\n                pq0.pop();\n                pq1.push(y);\n            }\n            if (pq1.size()>2)\n                pq1.pop();\n        }\n        int w=pq0.top();\n        pq0.pop();\n        w*=pq0.top();\n        int z=pq1.top();\n        pq1.pop();\n        z*=pq1.top();\n        return z-w;\n    }\n};\n```\n# C++ loop with if-else runs in 11 ms Beats 99.06%\n\n```\nclass Solution {\npublic:\n    int maxProductDifference(vector<int>& nums) {\n        int max0=1, max1=1, min1=INT_MAX, min0=INT_MAX;\n        for(int x: nums){\n            if (x>max0){\n                max1=max0;\n                max0=x; \n            }\n            else max1=max(max1, x);\n            if (x<min0) {\n                min1=min0;\n                min0=x;\n            }\n            else min1=min(min1, x);\n        }\n        return max0*max1-min0*min1;\n    }\n};\n```\n
['Sorting', 'Heap (Priority Queue)', 'C++', 'Python3']