{"id":6854,"date":"2020-05-31T09:18:17","date_gmt":"2020-05-31T16:18:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6854"},"modified":"2020-05-31T09:34:15","modified_gmt":"2020-05-31T16:34:15","slug":"leetcode-1464-maximum-product-of-two-elements-in-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1464-maximum-product-of-two-elements-in-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1464. Maximum Product of Two Elements in an Array"},"content":{"rendered":"\n<p>Given the array of integers&nbsp;<code>nums<\/code>, you will choose two different indices&nbsp;<code>i<\/code>&nbsp;and&nbsp;<code>j<\/code>&nbsp;of that array.&nbsp;<em>Return the maximum value of<\/em><code>(nums[i]-1)*(nums[j]-1)<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [3,4,5,2]\n<strong>Output:<\/strong> 12 \n<strong>Explanation:<\/strong> If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12. \n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,5,4,5]\n<strong>Output:<\/strong> 16\n<strong>Explanation:<\/strong> Choosing the indices i=1 and j=3 (indexed from 0), you will get the maximum value of (5-1)*(5-1) = 16.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [3,7]\n<strong>Output:<\/strong> 12\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= nums.length &lt;= 500<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10^3<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Sort<\/strong><\/h2>\n\n\n\n<p>We want to find the largest and second largest elements.<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxProduct(vector<int>& nums) {\n    sort(rbegin(nums), rend(nums));\n    return (nums[0] - 1) * (nums[1] - 1);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Without sorting<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxProduct(vector<int>& nums) {\n    int m1 = 0;\n    int m2 = 0;\n    for (int n : nums) {\n      if (n > m1) {\n        m2 = m1;\n        m1 = n;\n      } else if (n > m2) {\n        m2 = n;\n      }\n    }\n    return (m1 - 1) * (m2 - 1);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given the array of integers&nbsp;nums, you will choose two different indices&nbsp;i&nbsp;and&nbsp;j&nbsp;of that array.&nbsp;Return the maximum value of(nums[i]-1)*(nums[j]-1). Example 1: Input: nums = [3,4,5,2] Output: 12&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,228],"class_list":["post-6854","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-max-element","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6854"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6854\/revisions"}],"predecessor-version":[{"id":6856,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6854\/revisions\/6856"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}