Python/JS/Go/C++ O(n) by linear scan. [w/ Comment] 有中文解題文章
\u4E2D\u6587\u89E3\u984C\u6587\u7AE0\n\nO(n) by linear scan.\n\n---\n\nHint:\n\nMaintain a record of first largest number as well as second largest\n\n---\n\nIm
[\u4E2D\u6587\u89E3\u984C\u6587\u7AE0](https://vocus.cc/article/65780468fd897800013c9ad1)\n\nO(n) by linear scan.\n\n---\n\n**Hint**:\n\nMaintain a record of first largest number as well as second largest\n\n---\n\n**Implementation**:\n\nPython:\n\n```\nclass Solution(object):\n    def maxProduct(self, nums):\n\n        first, second = 0, 0\n        \n        for number in nums:\n            \n            if number > first:\n                # update first largest and second largest\n                first, second = number, first\n                \n            elif number > second:\n                # update second largest\n                second = number\n        \n        return (first - 1) * (second - 1)\n```\n\n---\n\nJavascript:\n\n```\nvar maxProduct = function(nums) {\n    \n    let [first, second] = [0, 0];\n    \n    for( const number of nums){\n        \n        if( number > first ){\n            [first, second] = [number, first];\n            \n        }else if( number > second ){\n            second = number;\n        }\n    } \n    \n    return ( first - 1 ) * ( second - 1 );\n    \n};\n```\n\n---\n\nGo:\n\n```\nfunc maxProduct(nums []int) int {\n    \n    first, second := 0, 0\n    \n    for _, number := range nums{\n        \n        if number > first{\n            first, second = number, first\n            \n        }else if number > second {\n            second = number\n        }\n    }\n    \n    return ( first - 1 )*( second - 1 )\n}\n```\n\n---\n\nC++\n\n```\nclass Solution {\npublic:\n    int maxProduct(vector<int>& nums) {\n        \n        int first = 0, second = 0;\n        \n        for( const int& number: nums){\n            \n            if( number > first ){\n                second = first;\n                first = number;\n                \n            }else if( number > second ){\n                second = number;\n            }\n            \n        }\n        \n        return ( first - 1 ) * ( second - 1 );\n    }\n};\n```
['Math', 'C', 'Iterator', 'Python', 'C++', 'Go', 'Python3', 'JavaScript']