LeetCode Brush Questions 1464. The maximum product of two elements in an array

LeetCode Brush Questions 1464. The maximum product of two elements in an array

I don't know where I am going, but I am already on my way!
Time is hurried, although I have never met, but I met Yusi, it is really a great fate, thank you for your visit!
  • Topic :
    give you an array of integers nums, you select two different array indices iand jso (nums[i]-1)*(nums[j]-1)obtain the maximum value.
    Please calculate and return the maximum value of this formula.
  • Example :
示例 1 :
输入:nums = [3,4,5,2]
输出:12 
解释:如果选择下标 i=1 和 j=2(下标从 0 开始),则可以获得最大值,(nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12 。 
示例 2 :
输入:nums = [1,5,4,5]
输出:16
解释:选择下标 i=1 和 j=3(下标从 0 开始),则可以获得最大值 (5-1)*(5-1) = 16 。
示例 3 :
输入:nums = [3,7]
输出:12
  • Tips :
    • 2 <= nums.length <= 500
    • 1 <= nums[i] <= 10^3
  • Code 1:
class Solution:
    def maxProduct(self, nums: List[int]) -> int:
        nums.sort()
        return (nums[-1]-1) * (nums[-2]-1)
# 执行用时 :52 ms, 在所有 Python3 提交中击败了34.16%的用户
# 内存消耗 :13.7 MB, 在所有 Python3 提交中击败了100.00%的用户
  • Algorithm description:
    Use the sorting function sort()to numssort, find the product of the largest value and the next largest value, (nums[-1]-1) * (nums[-2]-1)and return the result.
  • Code 2:
class Solution:
    def maxProduct(self, nums: List[int]) -> int:
        i = 0
        j = len(nums) - 1
        max_val = 0
        while i < j:
            temp = (nums[i]-1) * (nums[j]-1)
            if temp > max_val: max_val = temp
            if nums[i] > nums[j]: j -= 1
            else: i += 1
        return max_val
# 执行用时 :48 ms, 在所有 Python3 提交中击败了46.05%的用户
# 内存消耗 :13.5 MB, 在所有 Python3 提交中击败了100.00%的用户
  • Algorithm description:
    Double pointer method, initialize the maximum value max_val = 0, i = 0sum the two pointers before and after j = len(nums) - 1, traverse the array, find the product of the pointer index elements (nums[i]-1) * (nums[j]-1), compare with the maximum value, and update the maximum value. Compare the size of the pointer elements and update the pointer of the smaller element. The two pointers do not satisfy the size relationship, the loop ends, and the result is returned.
  • Code 3:
class Solution:
    def maxProduct(self, nums: List[int]) -> int:
        max_1 = max_2 = 1
        for i in range(len(nums)):
            if nums[i] >= max_1:
                max_2 = max_1
                max_1 = nums[i]
                continue
            if max_1 > nums[i] >= max_2:
                max_2 = nums[i]
        return (max_1 - 1) * (max_2 - 1)
# 执行用时 :36 ms, 在所有 Python3 提交中击败了94.58%的用户
# 内存消耗 :13.8 MB, 在所有 Python3 提交中击败了100.00%的用户
  • Algorithm description: The
    one-time traversal method initializes the maximum value and the second largest value max_1 = max_2 = 1. If the current element is not less than the maximum value if nums[i] >= max_1, assign the maximum value to the second largest value max_2 = max_1, assign the current element to the maximum value max_1 = nums[i], and continue the loop; if the current element is less than the maximum value, Not less than the second largest value if max_1 > nums[i] >= max_2, assign the current element to the second largest value max_2 = nums[i], and return the result (max_1 - 1) * (max_2 - 1).

Guess you like

Origin blog.csdn.net/qq_34331113/article/details/106649870