628. Maximum Product of Three [email protected]

Given an integer array, find three numbers whose product is maximum and output the maximum product.

Note:

  1. The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
  2. Multiplication of any three numbers in the input won‘t exceed the range of 32-bit signed integer.

原题地址: Maximum Product of Three Numbers

难度: Easy

题意: 找出相乘最大的三个数

思路:

因为数字有正有负,因此相乘最大的三个数分为两种情况:

(1)最大的三个正数

(2)最小的两个负数以及一个最大的正数

代码:

class Solution(object):
    def maximumProduct(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        a = b = c = None
        d = e = 0x7FFFFFFF
        for i in range(len(nums)):
            if nums[i] >= a:           # 找出最大的三个数
                a, b, c = nums[i], a, b
            elif nums[i] >= b:
                b, c = nums[i], b
            elif nums[i] >= c:
                c = nums[i]

            if nums[i] <= e:      # 找出最小的两个数    
                d, e = e, nums[i]
            elif nums[i] <= d:
                d = nums[i]

        max_val = 0

#         if a > 0 and b > 0 and c > 0:
#             max_val = max(max_val, a * b * c)

#         if a > 0 and d < 0 and e < 0:
#             max_val = max(max_val, a * d * e)

#         if a < 0 and b < 0 and c < 0:
#             max_val = a * b * c
        max_val = max(a*b*c, a*d*e)
        return max_val

时间复杂度: O(n)

空间复杂度: O(1)

原文地址:https://www.cnblogs.com/chimpan/p/9728933.html

时间: 2024-10-29 01:37:32

628. Maximum Product of Three [email protected]的相关文章

Leetcode - 628 Maximum Product of Three Numbers

Leetcode - 628 Maximum Product of Three Numbers 628. Maximum Product of Three Numbers Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4]

[Array]628. Maximum Product of Three Numbers

Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Output: 24 Note: The length of the given array will be in range [3,104] and all elemen

628. Maximum Product of Three Numbers

Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Output: 24 Note: The length of the given array will be in range [3,104] and all elemen

LeetCode 628. Maximum Product of Three Numbers三个数的最大乘积 (C++)

题目: Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Output: 24 分析: 给定一个数组,返回其中三个元素乘积的最大值. 注意的是,这道题是可以有负数出现,且是求三个数的乘积,所以我们需要考虑负数的情况. 最先

【[email&#160;protected]基础篇 ~】# 磁盘与文件系统

之前三篇文章我们简单介绍了Linux系统的用户管理,文件操作等,都是比较浅显的基本操作.这节我们要深入一下了,从文件系统我们要看到磁盘系统.从磁盘系统我们要看到操作系统的整体架构.废话不多少让我们开始学习吧! 磁盘与文件系统 1.磁盘系统 1.1 磁盘结构 如图所示,磁盘由扇区和柱面组成,分区的最小单位是柱面(柱是有厚度的,本图是截面图),磁盘读取的最小单位是扇区.第一扇区的MBR(446bytes)分区表可以最大包含四个分区(64bytes)的信息,即从开始柱面到结束柱面4组数据,每组16个字

【EBS】adpatch报错:libgcc_s.so: undefined reference to `[email&#160;protected]_2.4&#39;

EBS通过adpatch打补丁报错 /usr/lib/gcc/x86_64-redhat-linux/4.4.7/32/libgcc_s.so: undefined reference to `[email protected]_2.4' collect2: ld returned 1 exit status make: *** [/soft/ebs12/ERPDB/apps/apps_st/appl/ad/12.0.0/bin/adwrknew] Error 1 Done with link 

【[email&#160;protected]基础篇 ~】# 用户管理

经过前两篇文章的洗礼,相信大家对Linux的基本操作已有了大致的了解,今天我们来讲解一下Linux的用户管理,大家最熟悉的可能还是root用户,就是超级管理员么,具有神的权限.但是很多情况下,为了给其他用户登录和操作系统,我们需要创建其他非管理员用户并给他们分配一定的权限,以确保系统在多用户的情况下系统安全.那现在我们就开始学习吧,stay hungary, stay foolish! 用户管理 1.新建用户 1.1 创建一个普通账户 新建用户主要用useradd这个命令 [[email pro

[email&#160;protected]一个高效的配置管理工具--Ansible configure management--翻译(四)

无书面许可请勿转载 由于第三章内容较长,我将分做几个部分来翻译. Advanced Playbooks So far the playbooks that we have looked at are simple and just run a number of modules in order. Ansible allows much more control over the execution of your playbook. Using the following techniques

$*和[email&#160;protected]之间区别代码分析

#!/bin/bash set 'apple pie' pears peaches for i in $*           /*单引号被去掉,循环单个字符输出*/ do echo $i done [[email protected] Ex_14.02-14.31]# sh 14-14-1 apple pie pears peaches -------------------------------------------------------------- #!/bin/bash set