2 C++ 1 pass, priority_queue  O(1) vs 3 Python 1-line codes|| 0ms beats 100%
Intuition\n Describe your first thoughts on how to solve this problem. \nIterate the nums to find the max & 2nd max.\n\n2nd O(1) C++ solution uses priority_queu
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIterate the `nums` to find the max & 2nd max.\n\n2nd O(1) C++ solution uses priority_queue of size at most 3 which is also fast & beats 100%.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nPython codes are provided with  1-line codes.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$ vs $$O(n\\log n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n# Code 0ms beats 100%\n```\n#pragma GCC optimize("O3", "unroll-loops")\nclass Solution {\npublic:\n    int maxProduct(vector<int>& nums) {\n        int n=nums.size();\n        if (n<3) return (nums[0]-1)*(nums.back()-1);\n        int m0=nums[0],m1=nums[1];\n        if(m0<m1) swap(m0,m1);\n        for (int i=2;i<n;i++){\n            int x=nums[i];\n            if(x>m0){\n                m1=m0, m0=x;\n            }\n            else if(x>m1) m1=x;\n        }\n        return (m0-1)*(m1-1);\n    }\n};\nauto init = []()\n{ \n    ios::sync_with_stdio(0);\n    cin.tie(0);\n    cout.tie(0);\n    return \'c\';\n}();\n```\n# C++ using priority_queue|| 0ms beats 100%\n```\n#pragma GCC optimize("O3", "unroll-loops")\nclass Solution {\npublic:\n    int maxProduct(vector<int>& nums) {\n        priority_queue<int, vector<int>, greater<int>> pq;\n        for (int x: nums){\n            pq.push(x);\n            if (pq.size()>2) pq.pop();    \n        }\n        int product=pq.top()-1;\n        pq.pop();\n        product*=(pq.top()-1);\n        return product;\n    }\n};\n\n```\n# Python 1 line using sorted\n\n```\nclass Solution:\n    def maxProduct(self, nums: List[int]) -> int:\n        return ((x:=sorted(nums))[-1]-1)*(x[-2]-1)       \n```\n# Python 1 line 49ms beats 92.92%\n```\nclass Solution:\n    def maxProduct(self, nums: List[int]) -> int:\n        return ((x:=sorted(nums)[-2:])[-1]-1)*(x[-2]-1) \n```\n# Python 1 line using heap.nlargest\n```\nclass Solution:\n    def maxProduct(self, nums: List[int]) -> int:\n        return ((x:=heapq.nlargest(2, nums))[0]-1)*(x[1]-1)\n```
['Array', 'Sorting', 'Heap (Priority Queue)', 'C++', 'Python3']